如何使用 RSpec 的 an_instance_of 匹配器
目前尚不清楚为什么以下规范失败。如果我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来它正在接收带有实际 Network 类的消息,而不是它的实例。
It looks like it's receiving the message with the actual Network class and not an instance of it.