如何使用 RSpec 的 an_instance_of 匹配器 [英] How to use RSpec's an_instance_of matcher

查看:39
本文介绍了如何使用 RSpec 的 an_instance_of 匹配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不清楚为什么以下规范失败.如果我将 an_instance_of 更改为 nothing() 匹配器,则规范通过.我没有正确使用它吗?

It's unclear why the following spec is failing. If I change the an_instance_of to the anything() matcher the spec passes. Am I not using it correctly?

controller.should_receive(:authorize!).with(:show, an_instance_of(Network))
get :new


Failure/Error: get :new
       #<AffiliationsController:0x007ff5c607f238> received :authorize! with unexpected arguments
         expected: (:show, #<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x007ff5c6081600 @klass=Network(id: integer, creator_id: integer, name: string, zipcode: string, created_at: datetime, updated_at: datetime, allows_invitations: boolean, network_type_id: integer, active: boolean, fee: decimal, invitation_standard_text: text, description: text)>)
              got: (:show, Network(id: integer, creator_id: integer, name: string, zipcode: string, created_at: datetime, updated_at: datetime, allows_invitations: boolean, network_type_id: integer, active: boolean, fee: decimal, invitation_standard_text: text, description: text))

我正在尝试测试控制器是否正在调用 CanCan 的 load_and_authorize_resource 方法.理想情况下,我对这个发现抱有期望,但无法让它发挥作用.类似的东西:

I am trying to test that a controller is making a call to CanCan's load_and_authorize_resource method. I would ideally have an expectation on the find but couldn't get it to work. Something like:

network = stub
Network.should_receive(:new).and_return(stub)
controller.should_receive(:authorize!).with(:show, network)

推荐答案

看起来它正在接收带有实际 Network 类的消息,而不是它的实例.

It looks like it's receiving the message with the actual Network class and not an instance of it.

这篇关于如何使用 RSpec 的 an_instance_of 匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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