Jesper Rønn-Jensen
2007-Aug-21 19:35 UTC
How to access controller''s instance variables in integration tests?
In my controller''s action I set instance variables like: @last_action = xxx @paid_with_card = xxx Now, how do I access them in my integration tests? I tried Mike Clark''s example(1): assert_not_nil assigns(:paid_with_card) But my test fails: ============start test output=====Started ...................F.. Finished in 17.906 seconds. 1) Failure: test_select_and_buy_pack(AllPagesTest) [./test/integration/all_pages_test.rb:27:in `test_select_and_buy_pack'' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/ action_controller/integration.rb:453:in `run'']: <nil> expected to not be nil. 22 tests, 63 assertions, 2 failures, 0 errors ============end test output===== What can I do to access these variables? What do I get wrong here? What am I missing? How do you actually read instance variables from the controller in the integration tests????? Thanks in advance! /Jesper Rønn-Jensen, www.justaddwater.dk PS I have also tried these that don''t seem to work either: # controller.instance_variable_get("@paid_with_card") # controller.assigns(:paid_with_card) # controller.assigns[:paid_with_card] # assigns[:paid_with_card] (1) Mike Clark''s example: http://clarkware.com/cgi/blosxom/2006/04/04 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris T
2007-Aug-22 07:48 UTC
Re: How to access controller''s instance variables in integration tests?
Jesper Rønn-Jensen wrote:> In my controller''s action I set instance variables like: > > @last_action = xxx > @paid_with_card = xxx > > Now, how do I access them in my integration tests? I tried Mike > Clark''s example(1): > assert_not_nil assigns(:paid_with_card) > > But my test fails: > ============start test output=====> Started > ...................F.. > Finished in 17.906 seconds. > > 1) Failure: > test_select_and_buy_pack(AllPagesTest) > [./test/integration/all_pages_test.rb:27:in > `test_select_and_buy_pack'' > c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/ > action_controller/integration.rb:453:in `run'']: > <nil> expected to not be nil. > > > 22 tests, 63 assertions, 2 failures, 0 errors > ============end test output=====> > > What can I do to access these variables? What do I get wrong here? > What am I missing? How do you actually read instance variables from > the controller in the integration tests????? > > Thanks in advance! > /Jesper Rønn-Jensen, > www.justaddwater.dk > > PS I have also tried these that don''t seem to work either: > # controller.instance_variable_get("@paid_with_card") > # controller.assigns(:paid_with_card) > # controller.assigns[:paid_with_card] > # assigns[:paid_with_card] > > > (1) Mike Clark''s example: http://clarkware.com/cgi/blosxom/2006/04/04 > > > > > >Try controller.instance_variable_get(:@paid_with_card) Hope this helps, Chris -- ---------------------------- Autopendium :: Stuff about old cars http://autopendium.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jesper Rønn-Jensen
2007-Aug-22 12:30 UTC
Re: How to access controller''s instance variables in integration tests?
> Try > controller.instance_variable_get(:@paid_with_card) > Hope this helps, > ChrisThanks for your support Chris. I finally managed to get it working, and inspired by your tip I made an integration test to test off what works and whatnot. I have included the non-working examples as well for later reference. Kind regards, Jesper Rønn-Jensen http://justaddwater.dk/ PS. Actually I got it working with the first example I posted: assert_not_nil assigns(:paid_with_card) The catch was to put a> post_via_redirect :params > assert_not_nil assigns(:paid_with_card)instead of> post :params > assert_redirected_to > follow_redirect > assert_not_nil assigns(:paid_with_card)========file "/test/integration/test_of_assigns.rb" below===========require "#{File.dirname(__FILE__)}/../test_helper" # This test requires a root route pointed to a controller for the default action # controller#index responding to "/" # # Index action must contain setting of the instance variable @sub (value: anything but nil) # @sub # # Run this file separately with # ruby test_of_assigns.rb # class AssignOperatorTest < ActionController::IntegrationTest fixtures :whatever def test_front_page_1_works get "/" assert_response :success assert_not_nil assigns(:sub) end def test_front_page_2_works get "/" assert_not_nil controller.instance_variable_get("@sub") end def test_front_page_3_works get "/" assert_not_nil controller.instance_variable_get(:@sub) end def test_front_page_4_failure get "/" assert_not_nil controller.assigns[:sub] end def test_front_page_5_error get "/" assert_not_nil controller.instance_variable_get(:sub) end def test_front_page_6_error get "/" assert_not_nil controller.instance_variable_get[:sub] end def test_front_page_7_error get "/" assert_not_nil controller.assigns("@sub") end def test_front_page_8_error get "/" assert_not_nil controller.assigns(:sub) end end # # ruby script\about # About your application''s environment # Ruby version 1.8.6 (i386-mswin32) # RubyGems version 0.9.2 # Rails version 1.2.3 # Active Record version 1.15.3 # Action Pack version 1.13.3 # Action Web Service version 1.2.3 # Action Mailer version 1.3.3 # Active Support version 1.4.2 # Application root C:/Documents and Settings/Administrator/ My Documents/rails/tmp # Environment development # Database adapter sqlite3 # Database schema version 19 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---