95

What is the difference between an it block and a specify block in RSpec?

subject { MovieList.add_new(10) }

specify { subject.should have(10).items }
it { subject.track_number.should == 10}

They seem to do the same job. Just checking to be sure.

basheps
  • 10,034
  • 11
  • 36
  • 45

1 Answers1

121

The methods are the same; they are provided to make specs read in English nicer based on the body of your test. Consider these two:

describe Array do
  describe "with 3 items" do
    before { @arr = [1, 2, 3] }

    specify { @arr.should_not be_empty }
    specify { @arr.count.should eq(3) }
  end
end

describe Array do
  describe "with 3 items" do
    subject { [1, 2, 3] }

    it { should_not be_empty }
    its(:count) { should eq(3) }
  end
end
Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
  • 11
    You're correct, Brandon, `it` and `specify` are identical methods. You can see where they're defined [here in the source](https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb#L53-67). – Jordan Running Dec 13 '11 at 04:32
  • 1
    Excellent catch! Amazing what you can find from reading the source. :) I'll update the answer. – Michelle Tilley Dec 13 '11 at 04:34
  • 2
    Here is a gist with the examples methods names as of december 2013: https://gist.github.com/Dorian/7893586 (example, it, specify, focus, ...) – Dorian Dec 10 '13 at 16:35
  • 5
    [better rspec](http://betterspecs.org/) advise against the use of `should`, and in favor of `expect` – fotanus Feb 15 '14 at 22:58
  • 4
    UPDATE to the excellent link from @Jordan: https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb#L101-194 is now the spot where to find it. – Florian Eck Jul 01 '15 at 05:43
  • 1
    Just a heads up that `its(:count)` is no longer part of rspec core, see https://github.com/rspec/rspec-its and https://gist.github.com/myronmarston/4503509 – dentarg Dec 23 '15 at 11:34
  • guys, please use canonical links for github files, it's just only one keystroke: "y" – milushov Dec 11 '20 at 08:49