2013-05-29 32 views
1

我很新的測試rails應用程序。現在,我正在學習如何模擬方法。Rspec:模型中的update_attributes模型

我的方法是,在我的用戶模型更改密碼:

def change_password(user, pass) 
    self.update_attributes(user) if self.password_hash == self.encrypt_password(pass) 
    end 

所有我想要做的是模擬方法update_attributes方法。要做到這一點,我寫了一個測試:

let (:user) {FactoryGirl.build(:user)} 
subject{user} 
... 
describe "#change_password" do 
    before {user.save} 
    context "when old password doesn't match" do 
     it "should not update password" do 
     user.should_not_receive(:update_attributes) 
     user.change_password(user,"invalid") 
     end 
    end 
    end 

但是當我運行它,我得到錯誤:

NoMethodError: 
     undefined method `should_not_receive' for #<User:0x0000000455daf8> 

而且,我試圖嘲弄方法的update_attributes像這樣:

user.change_password.should_not_receive(:update_attributes) 

,我已經得到了另一個錯誤:

ArgumentError: 
     wrong number of arguments (0 for 2) 

我找不到有關如何在屬性方法中模擬方法的手冊。

有人能幫我解決我的問題嗎?

回答

0

要使用should_receive方法您需要設置config.mock_with :rspec但我已設置config.mock_with :mocha。所以我應該使用expects而不是should_receive