RSpec update_属性预期失败,即使更新有效
我使用的主要是脚手架生成的RSpec规范,它正在失败,但不应该失败。以下是规格:RSpec update_属性预期失败,即使更新有效,rspec,expectations,Rspec,Expectations,我使用的主要是脚手架生成的RSpec规范,它正在失败,但不应该失败。以下是规格: describe "PUT update" do describe "with valid params" do it "updates the requested invoice" do invoice = Invoice.create! Invoice.any_instance.should_receive(:update_attributes).with({
describe "PUT update" do
describe "with valid params" do
it "updates the requested invoice" do
invoice = Invoice.create!
Invoice.any_instance.should_receive(:update_attributes).with({"number" => "MyString" })
put :update, {:id => invoice.id, :invoice => { "number" => "MyString" }}
end
运行规范时,将在数据库中创建一张发票,并相应地进行更新。但是,我收到了这条信息,但失败了:
RSpec::Mocks::MockExpectationError: (#<Mocha::ClassMethods::AnyInstance:0x653a9a8>).update_attributes({"number"=>"MyString"})
expected: 1 time with arguments: ({"number"=>"MyString"})
received: 0 times with arguments: ({"number"=>"MyString"})
为什么会失败?与许多其他模拟框架一样,Mocha要求您在运行测试代码之前,将您的期望放在首位
因此,在测试中,交换两行;i、 e.呼叫Invoice.create!发票后。任何实例。应收到。冒着声明明显错误的风险,因为您收到了一个摩卡错误,在我看来,您需要禁用摩卡或将其配置为与RSpec一起使用 您可以通过从Gemfile中删除gem并重新执行bundle安装来禁用它。或者,您可以在指定gem时添加'require:false'参数,以便它不会自动加载
有关配置摩卡与RSpec配合使用的说明,请参见我遇到了一个类似的问题,我通过使用expects(期望)而不是Pould_receive(应该接收)解决了这个问题。您可能只需要将其更新为使用预期值,如下所示
describe "PUT update" do
describe "with valid params" do
it "updates the requested invoice" do
invoice = Invoice.create!
Invoice.any_instance.expects(:update_attributes).with({"number" => "MyString" })
put :update, {:id => invoice.id, :invoice => { "number" => "MyString" }}
end
end
end
您是否同时使用RSpec::Mocks和Mocha?Mocha gem已经安装。假设您想将Mocha与RSpec一起使用,您是否遵循了中的说明?事实上,安装的Mocha gem是我第一次设置项目时的遗留物,并且没有决定使用RSpec而不是我过去使用的minutest。RSpec未配置为使用摩卡。更新属性创新在探险队设置后已经开始,如下所示。显而易见的是答案。事实上,这就是问题所在。我应该知道的。我不清楚为什么摩卡会介入,因为RSpec应该使用自己的模拟设备。