出于某种原因,RSpec stub 在我的测试中不可用,我不明白为什么。
这是一个示例测试:
RSpec.describe User, type: :model do
it "Should allow me to stub a model" do
u = User.new
u.stub(:test)
u.test
end
end
这是测试结果:
Failures:
1) Subscription Should allow me to stub a model
Failure/Error: u.stub(:test)
NoMethodError:
undefined method `stub' for #<User:0x007fa5cfea20c8>
# ./spec/models/user_spec.rb:6:in `block (2 levels)
in <top (required)>'
FWIW 我的规范帮助程序文件的模拟配置部分如下:
config.mock_with :rspec do |mocks|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
mocks.syntax = :expect
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end
最佳答案
旧语法是 accessible with mocks.syntax = :should
。但是,您可以使用新的:allow(u).to receive(:test)
关于ruby-on-rails - 为什么无法向该对象添加 RSpec stub 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25566514/