Created
October 19, 2011 23:59
-
-
Save dbrady/1300039 to your computer and use it in GitHub Desktop.
Is this good RSpec style?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Totally agree. Though it must be said that for all practical purposes < 1% of the development population ever read them as documentation :)