Thursday, April 5, 2012

[Rails] rspec: identical tests fails when repeated

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.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate