I’m doing RSpec controller testing with CanCan authorization, and I’m
seeing something I’ve never seen in RSpec before: the same test run
twice fails on the second one. I am NOT doing before(:all) or other
things that should cause state to persist between tests:
Here’s the relevant code:
context “POST create” do
context "with user logged in" do
before(:each) do
@user = mock_model(User, :admin? => false, :guest? => false)
controller.stub(:current_user) { @user } # aka login
@attributes = valid_attributes
end
# succeeds
it "should create a new Premise" do
lambda{
post(:create, {:premise => @attributes}, :format => :json)
}.should change(Premise, :count).by(1)
end
# fails with CanCan::AccessDenied
it "should create a new Premise again" do
lambda{
post(:create, {:premise => @attributes}, :format => :json)
}.should change(Premise, :count).by(1)
end
end
I can’t figure out why the first test passes and the second test fails
– this suggests that there’s some state that’s being preserved across
the tests, but I can’t imagine what that might be, nor how to check for
it.
Any suggestions? (Of course, I can provide full model and Ability class
info if it would be helpful.) TIA.