2011-12-13 7 views
62

RSpecのitブロックと指定ブロックの違いは何ですか?RSpecのitブロックと指定ブロックの相違

subject { MovieList.add_new(10) } 

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

彼らは同じ仕事をしているようです。確かに確認するだけです。

答えて

85

この方法は、the sameです。彼らはあなたのテストのボディに基づいてより良い英語で読んで仕様を作るために提供されています。次の2つを考えてみましょう。

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 
+6

正しいですが、Brandon、「it」と「specify」は同じメソッドです。どこに定義されているのかを見ることができます(https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb#L53-67)。 –

+1

優れたキャッチ!あなたがソースを読むことで驚くべきものを見つけることができます。 :)私は答えを更新します。 –

+2

ここには、2013年12月現在のサンプルメソッド名の要点があります。https://gist.github.com/Dorian/7893586(例、it、specify、focus ...) – Dorian

関連する問題