Skip to content

Instantly share code, notes, and snippets.

@dbrady
Created October 19, 2011 23:59
Show Gist options
  • Select an option

  • Save dbrady/1300039 to your computer and use it in GitHub Desktop.

Select an option

Save dbrady/1300039 to your computer and use it in GitHub Desktop.
Is this good RSpec style?
# 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

Copy link
Copy Markdown

Totally agree. Though it must be said that for all practical purposes < 1% of the development population ever read them as documentation :)

@alindeman

Copy link
Copy Markdown

THEN I AM THE 1% ;) ;) ;)

@harukizaemon

Copy link
Copy Markdown

@dchelimsky

Copy link
Copy Markdown

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
end

This 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