[英]Rspec: stubbed model method still being called in controller test
In have this model in Rails: 在Rails中有此模型:
class User < ActiveRecord::Base
def self.create_auth_from_hash(hash)
# stuff
end
end
I want to stub the create_from_auth_hash
method so I can isolate the controller behaviour. 我想存根create_from_auth_hash
方法,以便可以隔离控制器行为。 Apparently the current syntax for this is: 显然,当前的语法是:
expect_any_instance_of(User).to receive(:create_from_auth_hash).and_return(false)
But even though I get no errors, the model method is still called. 但是,即使没有错误,模型方法仍然会被调用。
How can I stub model methods? 如何对模型方法存根? I'm using Rails 4.1 and RSpec 3.0. 我正在使用Rails 4.1和RSpec 3.0。
Your expectation is set up to stub a method on any instance of User
, not the class method you've defined on User
. 您的期望设置为在User
任何实例上存根方法,而不是在User
定义的类方法。
To do that, you just pass User
as the argument to expect
, instead of an instance. 为此,您只需将User
作为参数传递给expect
,而不是实例。 Like this: 像这样:
expect(User).to receive(:create_from_auth_hash).and_return(false)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.