[英]RSpec testing a destroy action with a conditional
I am trying to test a destroy action that has a conditional, it works fine if I just call the business without params like that:我正在尝试测试具有条件的销毁操作,如果我只调用没有这样的参数的业务,它就可以正常工作:
def destroy
if @message_rule.destroy
Messages::Reclassifier.call
end
But if I do it like that:但如果我这样做:
def destroy
if @message_rule.destroy
Messages::Reclassifier.call(allowed_params[:message])
end
It returns me this error:它返回给我这个错误:
Failures:
1) MessageRulesController#destroy When the message_rule has been destroyed classifies the messages
Failure/Error: expect(Messages::Reclassifier).to have_received(:call)
(Messages::Reclassifier (class)).call(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
# ./spec/controllers/message_rules_controller_spec.rb:108:in `block (4 levels) in <top (required)>'
This is the spec:这是规范:
describe '#destroy' do
let(:message_rule) { build_stubbed(:message_rule) }
before do
allow(Messages::Reclassifier).to receive(:call)
allow(MessageRule).to receive(:find).
with(message_rule.id.to_s).
and_return(message_rule)
end
subject(:destroy) { delete :destroy, params: { id: message_rule.id } }
context 'When the message_rule has been destroyed' do
before { allow(message_rule).to receive(:destroy).and_return(true) }
it 'classifies the messages' do
destroy
expect(Messages::Reclassifier).to have_received(:call)
end
end
context 'When the message_rule couldn\'t be destroyed' do
before { allow(message_rule).to receive(:destroy).and_return(false) }
it 'does not reclassify the messages' do
destroy
expect(Messages::Reclassifier).not_to have_received(:call)
end
end
end
Is there anything wrong I am doing in the specs?我在规范中做错了什么吗? I am unexperienced with RSpec so I am having a hard time understanding this error我对 RSpec 没有经验,所以我很难理解这个错误
The error is stating that Rspec expected Messages::Reclassifier
to call the method call
, but the method was not called.错误表明 Rspec 期望Messages::Reclassifier
调用方法call
,但未调用该方法。 This means that something is preventing the method call from happening.这意味着某些东西阻止了方法调用的发生。 You seem to have the method stub set up correctly.您似乎正确设置了方法存根。
Since you are stubbing Messages::Reclassifier#call
without the parameter option, it should not matter to Rspec whether or not you are passing in a parameter to Messages::Reclassifier#call
.由于您在没有参数选项的情况下存根Messages::Reclassifier#call
,因此 Rspec 是否将参数传递给Messages::Reclassifier#call
应该无关紧要。 But it would matter if the allowed_params[:message]
parameter is causing something like an exception.但是,如果allowed_params[:message]
参数导致异常之类的东西,那将很重要。
Can you provide the entire controller class or at least all code that pertains to the "destroy" action?您能否提供整个控制器类或至少所有与“销毁”操作相关的代码?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.