Stubbing Chained Methods with Rspec

后端 未结 5 1089
醉酒成梦
醉酒成梦 2021-02-05 22:06

I want to call a named_scope that will only return one record, but the named_scope returns an array, that\'s not a big deal as I can just chain it with .first:

M         


        
5条回答
  •  情话喂你
    2021-02-05 22:58

    Better version of

    Client.stub!(:named_scope).and_return(@clients = mock([Client]))
    @clients.stub!(:first).and_return(@client = mock(Client))
    

    will be:

    Client.should_receive(:named_scope).with(param).and_return do
      record = mock_model(Comm)
      record.should_receive(:do_something_else)
      [record]  
    end
    

提交回复
热议问题