Why doesn't devise related spec work?
First, let me say that login works correctly. The user is logged in for sure. I'm also certain that the post is happening properly (checked the message开发者_开发问答s and flushes, so i'm certain). And the real action of incrementing, as the test describes, works fine. Only the test fails.
But in this rspec below :
it "should increase the strength ability by one point and also update the strength_points by one if strength is the trained ability" do
@user.str = 10
@user.str_points = 0
post :train_ability, :ability => 'str'
flash[:error].should be_nil
@user.str_points.should == 1
@user.str.should == 11
end
The str and str_points shoulds fail. I'm actually using a login_user function in my macros(as specified in devise), like :
module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = :user
@user = Factory.create(:user)
sign_in @user
end
end
end
I'm sure that @user is indeed the current_user, but it seems that any attribute changes do not really happen to @user inside the spec(:user is a factory i created).
Why doesn't this work ? :/
First of all you haven't saved the @user
before posting to :train_ability
.
There's also a slim chance your @user
may be cached after that so reloading it before your assertions could be necessary.
Try changing your spec to the following
it "should increase the strength ability by one point and also update the strength_points by one if strength is the trained ability" do
@user.str = 10
@user.str_points = 0
@user.save! # save the @user object so str is 10 and str_points are 0
post :train_ability, :ability => 'str'
flash[:error].should be_nil
@user.reload # reload the user in case str and str_points are cached
@user.str_points.should == 1
@user.str.should == 11
end
精彩评论