Stubbing Chained Methods with Rspec

后端 未结 5 1094
醉酒成梦
醉酒成梦 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

    I think you've already done the thin controller thing by putting the query into a named scope where it can be reused. Here is some code I used before I started using named scopes.

      def mock_comm(stubs={})
        @mock_comm ||= mock_model(Comm, stubs)
      end
    
      describe "responding to GET index" do
    
        it "should expose all comms as @comms" do
          Comm.should_receive(:find).with(:all).and_return([mock_comm])
          get :index
          assigns[:comms].should == [mock_comm]
        end
    # ...
    

    I would probably write code quite similar to what you have already, but maybe put it in a helper that allows me to reuse it. The other thing is to use a different mocking framework that maybe gives you more control. Have a look at Ryan Bates' railscast on RSpec - it's a bit old now but still some good ideas in there.

提交回复
热议问题