如何使用RSpec的an_instance_of匹配器

时间:2012-02-24 03:59:09

标签: ruby-on-rails ruby-on-rails-3 rspec

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

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)

1 个答案:

答案 0 :(得分:1)

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