RSpec中的it块和指定块之间的差异

bas*_*eps 79 ruby rspec ruby-on-rails

RSpec中的it块和指定块之间有什么区别?

subject { MovieList.add_new(10) }

specify { subject.should have(10).items }
it { subject.track_number.should == 10}
Run Code Online (Sandbox Code Playgroud)

他们似乎做同样的工作.只是检查确定.

Mic*_*ley 105

方法是一样的 ; 提供它们是为了根据您的测试主体使用英语更好地阅读规范.考虑这两个:

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
Run Code Online (Sandbox Code Playgroud)

  • 你是对的,Brandon,`it`和`specified`是相同的方法.您可以看到它们的定义位置[在源代码中](https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb#L53-67). (8认同)
  • [更好的rspec](http://betterspecs.org/)建议不要使用`should`,并赞成`expect` (4认同)
  • 更新@Jordan的优秀链接:https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb#L101-194现在是找到它的地方. (4认同)
  • 以下是截至2013年12月的示例方法名称的要点:https://gist.github.com/Dorian/7893586(例如,它,指定,焦点,...) (2认同)