Rspec: How to block a private method?

Here is the error:

private method `desc' called for #<Array:0x0000010532e280> 

specification:

 describe SubjectsController do before(:each) do @subject = mock_model(Subject) end describe "#0002 - GET #index" do before(:each) do subjects = [@subject, mock_model(Subject), mock_model(Subject)] Subject.stub!(:all).and_return(subjects) Subject.all.stub!(:desc).and_return(subjects) get :index end it { response.should be_success } it { response.should render_template("index") } end end 

and controller:

 def index @subjects = Subject.all(conditions: {company_id: current_user.company.id}).desc(:created_at) end 

I do not know how to solve this problem, can someone give me a hand? Could you also give me advice on how you check this method? thanks.

+4
source share
2 answers

This is a little more mocking and solid than I prefer, but you can do it:

 subjects = [...] desc_mock = double("desc order mock") desc_mock.should_receive(:desc).with(:created_at).and_return(subjects) conditions = {...} Subject.should_receive(:all).with(conditions).and_return(desc_mock) 

You can also simplify this by moving the request to a named object that accepts some parameters. Then your test can confirm that the object received your area with the correct parameters, for example:

 Subject.should_receive(:user_company).with(current_user.id).and_return(subjects) 
+4
source

stub_chain should work here:

  Subject.stub_chain(:all,:desc) { [mock_model(Subject)]*2 } 
+2
source

All Articles