存根(...)收到带有(无参数)的意外消息(...) [英] Stub (...) received unexpected message (...) with (no args)

查看:100
本文介绍了存根(...)收到带有(无参数)的意外消息(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用RR编写测试.我需要的是模型对象的存根.

I try to write a test using RR. What I need is a stub of a model object.

describe ApplicationController do

  subject(:application_controller)     { ApplicationController.new }
  let(:messages)                       { ['a1', 'a2', 'a3' ] }
  let(:model)                          { Object.new }

  it 'should copy errors to flash' do
    stub(model).error_messages { messages }
    flash[:error] == nil
    subject.copy_errors_to_flash(model)
    flash[:error].should == messages
  end

end

我得到的是

ApplicationController should copy errors to flash
     Failure/Error: stub(model).error_messages { messages }
       Stub #<Object:0x007ffaa803f930> received unexpected message :error_messages with (no args)
     # ./spec/controllers/application_controller_spec.rb:10:in `block (2 levels) in <top (required)>'

我不知道我在做什么错.我想我遵循文档...

I have no idea what am I doing wrong. I think I follow the docs...

推荐答案

您正在此行的模型存根上调用方法'error_messages:

You're calling the method 'error_messages on the stub of your model on this line:

stub(model).error_messages { messages }

我认为您实际上想在这里做其他事情,很可能是这样:

I presume that you actually want to do something else here, most likely:

model.should_receive(:error_messages).and_return(messages)

这会为error_messages创建一个存根方法,并且每当您的规格测试调用model.error_messages时,就会使用您的message数组进行响应

which creates a stub method for error_messages and will respond with your messages array whenever your spec tests calls model.error_messages

这篇关于存根(...)收到带有(无参数)的意外消息(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆