[英]Rspec: mock update_attributes in model
I'm pretty new in testing rails applications. 我在测试Rails应用程序方面很新。 Now, I'm learning how to mock methods.
现在,我正在学习如何模拟方法。
I have method that changes password in my User model: 我有更改用户模型密码的方法:
def change_password(user, pass)
self.update_attributes(user) if self.password_hash == self.encrypt_password(pass)
end
All I want to do is to mock method update_attributes. 我要做的就是模拟方法update_attributes。 To do this, I wrote a test:
为此,我编写了一个测试:
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
But when I run it, I am getting error: 但是,当我运行它时,出现错误:
NoMethodError:
undefined method `should_not_receive' for #<User:0x0000000455daf8>
ALSO, I've tried to mock update_attributes method like this: 另外,我试图模拟update_attributes方法,如下所示:
user.change_password.should_not_receive(:update_attributes)
and I've got another mistake: 我还有另一个错误:
ArgumentError:
wrong number of arguments (0 for 2)
And I couldn't find any manual of how to mock methods inside methods with attributes. 而且我找不到关于如何在具有属性的方法中模拟方法的手册。
Could anybody help me with my problem,please? 有人可以帮我解决我的问题吗?
To use should_receive
method u need to set config.mock_with :rspec
but I've set config.mock_with :mocha
. 要使用
should_receive
方法,您需要设置config.mock_with :rspec
但我已经设置config.mock_with :mocha
。 So I should use expects
instead of should_receive
. 所以,我应该用
expects
代替should_receive
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.