norm
2009-Jul-28 17:44 UTC
[rspec-users] passing parameters when testing controller actions
My grad_surveys_controller has a student_edit method. An id parameter is passed in the url and a token is passed in a query string. For example: http://www.example.com/students/34/grad_surveys/21/student_edit?token=ab56e1b47 so the params hash should contain {"controller"=>"grad_surveys","action"=>"student_edit", "student_id"=>"34", "id"=>"21", "token"=>"ab56e1b47"} I am trying to test using the following example: it "should permit student access with the correct token supplied" do GradSurvey.stub!(:accepts_as_authorized).with (:id=>"1",:token=>"ab56e1b47").and_return(true) GradSurvey.should_receive(:accepts_as_authorized).with (:id=>"1", :token=>"ab56e1b47") get :student_edit, {:id=>"1", :token=>"ab56e1b47"} end which fails with the message: <GradSurvey(... snip... all the fields listed) (class)> expected :accepts_as_authorized with ({:token=>"ab56e1b47", :id=>"1"}) but received it with ("1", "ab56e1b47") What is the error in my example syntax that is causing the parameters not to be correctly recognized. thanks in advance for any assistance you are able to providd
David Chelimsky
2009-Jul-29 12:10 UTC
[rspec-users] passing parameters when testing controller actions
On Tue, Jul 28, 2009 at 1:44 PM, norm<codehacker at comcast.net> wrote:> My grad_surveys_controller has a student_edit method. An id parameter > is passed in the url and a token is passed in a query string. For > example: > ? ? ? ? ?http://www.example.com/students/34/grad_surveys/21/student_edit?token=ab56e1b47 > so the params hash should contain > {"controller"=>"grad_surveys","action"=>"student_edit", > "student_id"=>"34", "id"=>"21", "token"=>"ab56e1b47"} > > I am trying to test using the following example: > > ?it "should permit student access with the correct token supplied" do > ? ?GradSurvey.stub!(:accepts_as_authorized).with > (:id=>"1",:token=>"ab56e1b47").and_return(true) > ? ?GradSurvey.should_receive(:accepts_as_authorized).with > (:id=>"1", :token=>"ab56e1b47")Why are you using both stub!() and should_receive() here? The should_receive() call essentially overrides stub!(), so stub!() has no effect in this example.> ? ?get :student_edit, {:id=>"1", :token=>"ab56e1b47"} > ?end > > which fails with the message: > <GradSurvey(... snip... all the fields listed) (class)> > expected :accepts_as_authorized with ({:token=>"ab56e1b47", :id=>"1"}) > but received it with ("1", "ab56e1b47") > > What is the error in my example syntax that is causing the parameters > not to be correctly recognized. > > thanks in advance for any assistance you are able to provideThe error says that it''s expecting {:token=>"ab56e1b47", :id=>"1"}, which looks correct based on the line that''s setting up the expectation. So I think the problem may be with the code, not the spec. Can you post the controller code?
norm
2009-Jul-29 12:51 UTC
[rspec-users] passing parameters when testing controller actions
wow, thanks for the tips David. I''m using both the stub!() and the should_receive() because, as a complete RSpec neophyte, that''s what I thought was required! (And it seemed to work, too!). I didn''t suspect the controller code, because it''s code that works. But with your suggestion, I found the discrepancy between the controller code and the spec... the error message was telling me but I wasn''t seeing it!
David Chelimsky
2009-Jul-29 12:53 UTC
[rspec-users] passing parameters when testing controller actions
On Wed, Jul 29, 2009 at 8:51 AM, norm<codehacker at comcast.net> wrote:> wow, thanks for the tips David. I''m using both the stub!() and the > should_receive() because, as a complete RSpec neophyte, that''s what I > thought was required! (And it seemed to work, too!). > > I didn''t suspect the controller code, because it''s code that works. > But with your suggestion, I found the discrepancy between the > controller code and the spec... the error message was telling me but I > wasn''t seeing it!Glad to hear you''ve got it sorted out. Cheers, David