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

describe "#extractor_name" do
  context "with extractor name set" do
    subject { Migratrix::Transforms::Transform.new(:pants_transform, { "extractor_name" => :pants_extractor }) }
    its(:extractor_name) { should == :pants_extractor }
  end

  context "without extractor name set" do
    subject { Migratrix::Transforms::Transform.new(:pants_transform) }
    its(:extractor_name) { should == :pants_transform }
  end
end

@alindeman
Copy link

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"

@harukizaemon
Copy link

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

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

@harukizaemon
Copy link

@dchelimsky
Copy link

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