Reputation: 2258
I'm basing my question on this one: rspec mocks: verify expectations in it "should" methods? and also on these guidelines: http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/
So, if there should be only one expectation per "it" block, what if I'm mixing mocks expectations with value expectations?
Example:
describe Foo, "#bar" do
before :all do
@baz = mock(Baz)
@foo = Foo.new(@baz)
end
it "should call waka on @baz" do
@baz.should_receive(:waka)
end
it "should not be empty afterwards" do
@foo.should_not be_empty
end
it "should not raise exceptions" do
lambda {@foo.bar}.should_not raise_exception # potentially with side effects?
end
after :all do
@foo.bar # won't work with the second expectation
end
end
I hope the problem is clear. Does anybody has experience with organizing specs on this case? Am I doing some mistake?
Upvotes: 0
Views: 316
Reputation: 15552
One expectation per group is a suggestion, not a hard and fast rule. You shouldn't go crazy trying to conform to it (and in this case, I think using an after :all
block is going a little crazy).
I'm also getting rid of the before :all
and just using before :each
- :all can occasionally lead to confusing state bugs.
describe Foo, "#bar" do
before do
@baz = mock(Baz)
@foo = Foo.new(@baz)
end
it "should call waka on @baz" do
@baz.should_receive(:waka)
@foo.bar
end
it "should not be empty afterwards" do
@foo.bar
@foo.should_not be_empty
end
it "should not raise exceptions" do
lambda { @foo.bar }.should_not raise_exception
end
end
And actually, you're still only setting one expectation per group.
Upvotes: 4