-
-
Save dbrady/1300039 to your computer and use it in GitHub Desktop.
| # RSpec style question for you here. | |
| # | |
| # Here is the code being tested: | |
| # | |
| # def extractor_name | |
| # options[:extractor_name] || name | |
| # end | |
| # | |
| # My question is, I feel like I'm doing a lot of setup and | |
| # contexting just to write a pair of single-line specs. What do you | |
| # think of this style, and if you don't like it how would you | |
| # improve it? (If your answer is to not use RSpec, please include a | |
| # non-trolling reason, e.g. show me a different structure, not just | |
| # a port of this spec to Minitest. :-) ) | |
| describe "#extractor_name" do | |
| context "with extractor name set" do | |
| let(:transform) { Migratrix::Transforms::Transform.new(:pants_transform, { "extractor_name" => :pants_extractor })} | |
| it "returns extractor name" do | |
| transform.extractor_name.should == :pants_extractor | |
| end | |
| end | |
| context "without extractor name set" do | |
| let(:transform) { Migratrix::Transforms::Transform.new(:pants_transform)} | |
| it "returns transform name" do | |
| transform.extractor_name.should == :pants_transform | |
| end | |
| end | |
| end |
harukizaemon
commented
Oct 20, 2011
I have mixed feelings about its. I feel like if it's dead simple obvious what should be happening, it is OK and really nicely concise .. but if it's ever unclear in the least, it's better to use it with a string that documents the behavior.
My tip: make sure your specs look great when run with --format=doc
For instance, your style looks fine but I might expand on some of the "docs" that the tests provide .. like it "returns transform name as a default"
Totally agree. Though it must be said that for all practical purposes < 1% of the development population ever read them as documentation :)
THEN I AM THE 1% ;) ;) ;)
Hehe. Yes, I suspect we are - http://www.harukizaemon.com/2010/02/tests-as-documentation.html
let, context, and subject are all tools you can use to reduce duplication in a uniform way, but using them off the bat is a lot like saying "let's use the Factory and Strategy patterns to figure out what day of the week it is."
Why not just start off like this?
describe Migratrix::Transforms::Transform do
it "#extractor_name returns extractor name when set" do
transform = Migratrix::Transforms::Transform.new(:pants_transform, { "extractor_name" => :pants_extractor })
transform.extractor_name.should eq(:pants_extractor)
end
it "#extractor_name returns transform name when no extractor name is set" do
transform = Migratrix::Transforms::Transform.new(:pants_transform)
transform.extractor_name.should eq(:pants_transform)
end
endThis is quite readable just like that. At the point that you add a 2nd example of what happens when you set the extractor name, then you might add the context.
Although I can't help thinking that if everybody had a good pants extractor, these sorts of questions would just go away.