[rspec-users] stubbing association methods in controller spec

norm codehacker at comcast.net
Thu Jul 30 19:13:30 EDT 2009


I have two models:
  Student has_many=>:grad_surveys
    and
  GradSurvey belongs_to=>:student

I am testing the GradSurveysController#update method with

  it "should only allow update if the correct token is presented" do
    @params = {:id=>"1", :token=>"ab56e1b47", :survey=>{}}
    @mock_grad_survey = mock_model(GradSurvey)
    GradSurvey.should_receive(:accepts_as_authorized).with(@params
[:id], at params[:token]).and_return(@mock_grad_survey)
    put :update, @params
    response.should render_template(:update)
  end

Inside the GradSurvey#accepts_as_authorized method is called the
GradSurvey#student method. The test above fails with the message:
         Mock 'GradSurvey_1034' received unexpected message :student
with (no args)

However I can make the test pass by adding the :student method to the
@mock_grad_survey object, so
      @mock_grad_survey = mock_model(GradSurvey, :student=>mock_model
(Student))

Why is it necessary for me to explicitly add the #student method to
the mock GradSurvey model, it seems to me that it should come for free
as part of the defined association, no? What am I not understanding
here?

thanks in advance for any light you are able to shed


More information about the rspec-users mailing list