- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
由于 rubocop 错误,我想避免在我的规范中使用 allow_any_instance_of
。
我正在尝试改变这个:
before do
allow_any_instance_of(ApplicationController).to receive(:maintenance_mode_active?).and_return(false)
end
it 'returns http success' do
expect(response).to have_http_status(:success)
end
根据rubocop-docs它应该是这样的:
let(:maintenance_mode?) { instance_double(ApplicationController) }
before do
allow(ApplicationController).to receive(:maintenance_mode_active?).and_return(false)
allow(maintenance_mode?).to receive(:maintenance_mode_active?)
end
it 'returns http success' do
expect(response).to have_http_status(:success)
end
我要测试的类:
private
def check_maintenance?
if maintenance_mode_active? == true
redirect_to maintenance_mode
elsif request.fullpath.include?(maintenance_mode_path)
redirect_to :root
end
end
def maintenance_mode_active?
# do sth ...
mode.active?
end
上面的代码出现错误:
2.1) Failure/Error: allow(ApplicationController).to receive(maintenance_mode?).and_return(false)
#<InstanceDouble(ApplicationController) (anonymous)> received unexpected message :to_sym with (no args)
# ./spec/controllers/static_pages_controller_spec.rb:15:in `block (4 levels) in <top (required)>'
2.2) Failure/Error: allow(ApplicationController).to receive(maintenance_mode?).and_return(false)
TypeError:
[#<RSpec::Mocks::MockExpectationError: #<InstanceDouble(ApplicationController) (anonymous)> received unexpected message :to_sym with (no args)>] is not a symbol nor a string
# ./spec/controllers/static_pages_controller_spec.rb:15:in `block (4 levels) in <top (required)>'
最佳答案
您必须 stub ApplicationController
以返回 instance_double(ApplicationController)
:
let(:application_controller) { instance_double(ApplicationController) }
allow(ApplicationController).to receive(:new).and_return(application_controller)
allow(application_controller).to receive(:maintenance_mode_active?).and_return(false)
关于ruby-on-rails - Rspec 如何避免 allow_any_instance_of,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58772889/
假设我们有以下代码: class A def create_server options = { name: NameBuilder.new.build_name }
我的 mock 只有在如下所示的 before block 中时才有效。这只是我对我的问题的快速而肮脏的表述。从字面上看,当我将行从 before block 移动到 does not quack 断
由于 rubocop 错误,我想避免在我的规范中使用 allow_any_instance_of。 我正在尝试改变这个: before do allow_any_instance_of(A
在 the documentation Rspec Mocks 维护者 ... discourage its use for a number of reasons... 列出的原因似乎与 expec
我是 RSpec 的新手,正在测试一些带有请求类型的 webhook 测试。但即使我在这里使用 allow_any_instance_of,它也会出错 got 500 instead of 200。我
我在学习 rspec stub 的工作原理时遇到了一些麻烦。 我必须测试以下辅助方法,我希望将输出字符串测试为 html: def build_links(resource) YAML.load_
有没有可能做这样的事情??? allow_any_instance_of(Object).to receive(:foo).and_return("hello #{instance.id}") 我可以
我有一个简单的 MySQL 包装类,它将运行查询并返回结果。 class Rsql def initialize(db) @client = Mysql2::Client @db
我有下一个问题。当我尝试使用 stub ActiveRecord 模型的实例方法时allow_any_instance_of 我收到错误消息“模型未实现#method”,但是如果我在此 stub 之前
我是一名优秀的程序员,十分优秀!