如何使用 RSpec 的 an_instance_of 匹配器

发布于 2025-01-08 16:36:54 字数 1209 浏览 1 评论 0原文

目前尚不清楚为什么以下规范失败。如果我将 an_instance_of 更改为 everything() 匹配器,则规范将通过。是我使用方法不正确吗?

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 方法。理想情况下,我对这一发现抱有期望,但无法让它发挥作用。像这样的东西:

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

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))

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2025-01-15 16:36:54

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文