问题描述
|
我正在使用地理编码器gem向我的Active Record模型类之一添加地理编码功能。这很好用,但是我实际上不希望在单元测试期间触发地理编码。
我尝试通过将其添加到我的RSpec测试中来解决对地理编码的调用:
之前(:每个)做
User.stub!(:geocode).and_return([1,1])
结束
但是,当我运行测试时,它似乎仍在呼唤地理编码。我究竟做错了什么?
仅供参考,如果我在实例级别上存根(例如some_user.stub!而不是User.stub!),那么所有这些都可以使用。
解决方法
如果要在实例级别使用存根,则应使用RSpec以外的其他模拟框架。例如,它是“ 0”(将以下内容添加到“ 1”):
Spec::Runner.configure do |config|
config.mock_with :mocha
end
http://rspec.info/documentation/mocks/other_frameworks.html
现在,您可以在测试中使用any_instance
:
before(:each) do
User.any_instance.stub(:geocode).and_return([1,1])
end
,它的
before(:each) do
Address.any_instance.stubs(:geocode).returns([1,1])
end
与摩卡咖啡。