我是 rspec 的新手,我遇到了一些问题。有人可以帮助我吗?
我有一个负责停用用户的控制器操作。我试图用 rspec 测试来覆盖它,但结果不是我正在等待的。
控制器:
def deactivate
@user = User.find(params[:id])
if !@user.nil?
@user.update_attribute(:active, false)
redirect_to users_url
end
end
控制器规格
describe "PUT #deactivate" do
describe "with valid parameters" do
before (:each) do
@user = mock_model(User, :id => 100, :login => "login", :password => "password123",
:email => "email@gmail.com", :active => true)
User.should_receive(:find).with("100").and_return(@user)
end
it "should deactivate an user" do
@user.stub!(:update_attribute).with(:active, false).and_return(true)
put :deactivate, :id => "100"
@user.active.should eq false
end
end
end
测试结果:
1) UsersController PUT #deactivate with valid parameters should deactivate an user
Failure/Error: @user.active.should eq false
expected: false
got: true
(compared using ==)
所以,我不明白为什么 active 属性在应该为假时仍然为真。有任何想法吗 ?
谢谢!