I''m trying to test /foo/create action. I read all the testing howto and also this: http://wiki.rubyonrails.com/rails/show/HowtoFunctionalTestACreateAction but I can''t get it work. post() passes but assert_redirect_to redirects elsewhere (due to false return value of foo.save), my problem is... how can i know what''s happening into the test function? Tried using a breakpoint statement but since the object has not been saved in the model i don''t know how to look at it. Any solution? -- Lawrence http://www.oluyede.org/blog
Lawrence Oluyede wrote:> my > problem is... how can i know what''s happening into the test function? > Tried using a breakpoint statement but since the object has not been > saved in the model i don''t know how to look at it. > > Any solution?A couple of thoughts: If your model fails to save when given specific values you can unit test the model with those values outside the scope of the controller(IE a unit test instead of a functional test). Still, you can probably track down the unsaved record in the function test, check within the @response object. Also, you can call a breakpoint anywhere you want, so you might get a better vantage point on whats going on by breakpointing from within the controller being tested or even the model itself. hth. -- Lee
2005/5/11, Lee O''Mara <lee-O8glSrxzjJo@public.gmane.org>:> A couple of thoughts: > > If your model fails to save when given specific values you can unit test > the model with those values outside the scope of the controller(IE a > unit test instead of a functional test).Right, I already have some unit testing for the model. Gotta improve it. Turn back to fixtures :)> Still, you can probably track down the unsaved record in the function > test, check within the @response object.Ah, ok. Thanks! -- Lawrence http://www.oluyede.org/blog
I''m trying to dig into @response test object (test the same stuff in the model works) and understand how functional test works but I don''t get it :( here''s my object in a breakpoint right after the "post" method irb(test_create(PasteControllerTest)):001:0> @response => #<ActionController::TestResponse:0xb7541074 @redirected_to={:action=>"index"}, @headers={"Status"=>"302 Found", "cookie"=>[], "Cache-Control"=>"no-cache", "location"=>"http://test.host/"}, @template=#<ActionView::Base:0xb753faf8 @base_path="./test/functional/../../config/..//app/views/", @controller=#<PasteController:0xb7541150 @action_methods=["set_author_cookie", "create", "index", "show", "wsdl", "download", "rescue_action"], @performed_redirect=true, @performed_render=false, @author_cookie_val=nil, @headers={"Status"=>"302 Found", "cookie"=>[], "Cache-Control"=>"no-cache", "location"=>"http://test.host/"}, @request=#<ActionController::TestRequest:0xb7541128 @path=nil, @path_parameters={:action=>"create", :controller=>"paste"}, @request_uri=nil, @host="test.host", @parameters={"paste"=>{"created_on"=>"Wed May 11 19:39:42 CEST 2005", "body"=>"def foo end", "language"=>"Ruby", "author"=>"anonymous", "description"=>"test"}, "action"=>"create", "controller"=>"paste"}, @env={"REQUEST_URI"=>"/paste/create?paste=created_onWed+May+11+19%3A39%3A42+CEST+2005bodydef+foo+endlanguageRubyauthoranonymousdescriptiontest", "SERVER_PORT"=>80, "REQUEST_METHOD"=>"POST"}, @request_parameters={}, @remote_ip=nil, @cookies={}, @session=#<ActionController::TestSession:0xb7540fc0 @attributes={"flash"=>{}}>, @query_parameters={"action"=>"create"}, @remote_addr="127.0.0.1">, @cookies={}, @paste=#<Paste:0xb753d2d0 @attributes={"created_on"=>nil, "language"=>nil, "body"=>nil, "author"=>nil, "description"=>nil}, @errors=#<ActiveRecord::Errors:0xb753b3b8 @base=#<Paste:0xb753d2d0 ...>, @errors={"body"=>["can''t be empty"], "language"=>["is too long (max is 20 characters)", "can''t be empty"], "author"=>["is too long (max is 20 characters)", "can''t be empty"], "description"=>["is too long (max is 50 characters)"]}>, @new_record=true>, @template=#<ActionView::Base:0xb753faf8 ...>, @session=#<ActionController::TestSession:0xb7540fc0 @attributes={"flash"=>{}}>, @response=#<ActionController::TestResponse:0xb7541074 ...>, @params={"paste"=>{"created_on"=>"Wed May 11 19:39:42 CEST 2005", "body"=>"def foo end", "language"=>"Ruby", "author"=>"anonymous", "description"=>"test"}, "action"=>"create", "controller"=>"paste"}, @url=#<ActionController::UrlRewriter:0xb753faa8 @parameters={"paste"=>{"created_on"=>"Wed May 11 19:39:42 CEST 2005", "body"=>"def foo end", "language"=>"Ruby", "author"=>"anonymous", "description"=>"test"}, "action"=>"create", "controller"=>"paste"}, @request=#<ActionController::TestRequest:0xb7541128 @path=nil, @path_parameters={:action=>"create", :controller=>"paste"}, @request_uri=nil, @host="test.host", @parameters={"paste"=>{"created_on"=>"Wed May 11 19:39:42 CEST 2005", "body"=>"def foo end", "language"=>"Ruby", "author"=>"anonymous", "description"=>"test"}, "action"=>"create", "controller"=>"paste"}, @env={"REQUEST_URI"=>"/paste/create?paste=created_onWed+May+11+19%3A39%3A42+CEST+2005bodydef+foo+endlanguageRubyauthoranonymousdescriptiontest", "SERVER_PORT"=>80, "REQUEST_METHOD"=>"POST"}, @request_parameters={}, @remote_ip=nil, @cookies={}, @session=#<ActionController::TestSession:0xb7540fc0 @attributes={"flash"=>{}}>, @query_parameters={"action"=>"create"}, @remote_addr="127.0.0.1">>, @assigns={"flash"=>{}}>, @assigns={"flash"=>{}}>, @session=#<ActionController::TestSession:0xb7540fc0 @attributes={"flash"=>{}}>, @body="<html><body>You are being <a href=\"http://test.host/\">redirected</a>.</body></html>", @assigns=[]> irb(test_create(PasteControllerTest)):002:0> @response.template => #<ActionView::Base:0xb753faf8 @base_path="./test/functional/../../config/..//app/views/", @controller=#<PasteController:0xb7541150 @action_methods=["set_author_cookie", "create", "index", "show", "wsdl", "download", "rescue_action"], @performed_redirect=true, @performed_render=false, @author_cookie_val=nil, @headers={"Status"=>"302 Found", "cookie"=>[], "Cache-Control"=>"no-cache", "location"=>"http://test.host/"}, @request=#<ActionController::TestRequest:0xb7541128 @path=nil, @path_parameters={:action=>"create", :controller=>"paste"}, @request_uri=nil, @host="test.host", @parameters={"paste"=>{"created_on"=>"Wed May 11 19:39:42 CEST 2005", "body"=>"def foo end", "language"=>"Ruby", "author"=>"anonymous", "description"=>"test"}, "action"=>"create", "controller"=>"paste"}, @env={"REQUEST_URI"=>"/paste/create?paste=created_onWed+May+11+19%3A39%3A42+CEST+2005bodydef+foo+endlanguageRubyauthoranonymousdescriptiontest", "SERVER_PORT"=>80, "REQUEST_METHOD"=>"POST"}, @request_parameters={}, @remote_ip=nil, @cookies={}, @session=#<ActionController::TestSession:0xb7540fc0 @attributes={"flash"=>{}}>, @query_parameters={"action"=>"create"}, @remote_addr="127.0.0.1">, @cookies={}, @paste=#<Paste:0xb753d2d0 @attributes={"created_on"=>nil, "language"=>nil, "body"=>nil, "author"=>nil, "description"=>nil}, @errors=#<ActiveRecord::Errors:0xb753b3b8 @base=#<Paste:0xb753d2d0 ...>, @errors={"body"=>["can''t be empty"], "language"=>["is too long (max is 20 characters)", "can''t be empty"], "author"=>["is too long (max is 20 characters)", "can''t be empty"], "description"=>["is too long (max is 50 characters)"]}>, @new_record=true>, @template=#<ActionView::Base:0xb753faf8 ...>, @session=#<ActionController::TestSession:0xb7540fc0 @attributes={"flash"=>{}}>, @response=#<ActionController::TestResponse:0xb7541074 @redirected_to={:action=>"index"}, @headers={"Status"=>"302 Found", "cookie"=>[], "Cache-Control"=>"no-cache", "location"=>"http://test.host/"}, @template=#<ActionView::Base:0xb753faf8 ...>, @session=#<ActionController::TestSession:0xb7540fc0 @attributes={"flash"=>{}}>, @body="<html><body>You are being <a href=\"http://test.host/\">redirected</a>.</body></html>", @assigns=[]>, @params={"paste"=>{"created_on"=>"Wed May 11 19:39:42 CEST 2005", "body"=>"def foo end", "language"=>"Ruby", "author"=>"anonymous", "description"=>"test"}, "action"=>"create", "controller"=>"paste"}, @url=#<ActionController::UrlRewriter:0xb753faa8 @parameters={"paste"=>{"created_on"=>"Wed May 11 19:39:42 CEST 2005", "body"=>"def foo end", "language"=>"Ruby", "author"=>"anonymous", "description"=>"test"}, "action"=>"create", "controller"=>"paste"}, @request=#<ActionController::TestRequest:0xb7541128 @path=nil, @path_parameters={:action=>"create", :controller=>"paste"}, @request_uri=nil, @host="test.host", @parameters={"paste"=>{"created_on"=>"Wed May 11 19:39:42 CEST 2005", "body"=>"def foo end", "language"=>"Ruby", "author"=>"anonymous", "description"=>"test"}, "action"=>"create", "controller"=>"paste"}, @env={"REQUEST_URI"=>"/paste/create?paste=created_onWed+May+11+19%3A39%3A42+CEST+2005bodydef+foo+endlanguageRubyauthoranonymousdescriptiontest", "SERVER_PORT"=>80, "REQUEST_METHOD"=>"POST"}, @request_parameters={}, @remote_ip=nil, @cookies={}, @session=#<ActionController::TestSession:0xb7540fc0 @attributes={"flash"=>{}}>, @query_parameters={"action"=>"create"}, @remote_addr="127.0.0.1">>, @assigns={"flash"=>{}}>, @assigns={"flash"=>{}}> It seems that at some time the object attributes become nil but why... and when? -- Lawrence http://www.oluyede.org/blog
Lawrence Oluyede wrote:> I''m trying to dig into @response test object (test the same stuff in > the model works) and understand how functional test works but I don''t > get it :( > > here''s my object in a breakpoint right after the "post" method > > irb(test_create(PasteControllerTest)):001:0> @response > => #<ActionController::TestResponse:0xb7541074<meca-snip>> > It seems that at some time the object attributes become nil but why... and when? >I can''t really make sense of the exact problem you''re having from the response, but one thing did stand out. The @response.env contains the following: @env={ "REQUEST_URI"=>"/paste/create?paste=created_onWed+May+11+19%3A39%3A42+CEST+2005bodydef+foo+endlanguageRubyauthoranonymousdescriptiontest", "SERVER_PORT"=>80, "REQUEST_METHOD"=>"POST" } Note that the request_uri is carrying your params on the url (as in a GET request) but you''re testing with the POST method. I don''t know if it''s directly related to your error, but it doesn''t seem right. -- Lee
Lawrence Oluyede
2005-May-11 22:05 UTC
functional testing bug? (was Re: [Rails] create and functional testing)
2005/5/11, Lee O''Mara <lee-O8glSrxzjJo@public.gmane.org>:> I can''t really make sense of the exact problem you''re having from the > response, but one thing did stand out. > > The @response.env contains the following: > @env={ > > "REQUEST_URI"=>"/paste/create?paste=created_onWed+May+11+19%3A39%3A42+CEST+2005bodydef+foo+endlanguageRubyauthoranonymousdescriptiontest", > > "SERVER_PORT"=>80, > "REQUEST_METHOD"=>"POST" > } > > Note that the request_uri is carrying your params on the url (as in a > GET request) but you''re testing with the POST method. I don''t know if > it''s directly related to your error, but it doesn''t seem right. >I tried calling post() manually in a debug session (with breakpoint) and I get the same problem. I dug into rails/actionpack/lib/action_controller/test_process.rb but I''m not able to know if it''s a bug or not. What I''m missing? This is the code snippet (placed in the right place in a test_ method into the functional tester file) post :create, { :paste => { :author => "anonymous", :language => "Ruby", :description => "test", :body => "def foo end", :created_on => Time.now } } -- Lawrence http://www.oluyede.org/blog
Lee O''Mara
2005-May-11 22:15 UTC
Re: functional testing bug? (was Re: [Rails] create and functional testing)
Lawrence Oluyede wrote:> 2005/5/11, Lee O''Mara <lee-O8glSrxzjJo@public.gmane.org>: > >> >> >>Note that the request_uri is carrying your params on the url (as in a >>GET request) but you''re testing with the POST method. I don''t know if >>it''s directly related to your error, but it doesn''t seem right. >> > > I tried calling post() manually in a debug session (with breakpoint) > and I get the same problem. I dug into > rails/actionpack/lib/action_controller/test_process.rb but I''m not > able to know if it''s a bug or not. What I''m missing?Strange. I''m not sure either. Does breakpointing from within the controllers context yield any useful info? Eg: Does @params contain the paste hash? can you create and save a model with it? -- Lee
Lawrence Oluyede
2005-May-11 22:21 UTC
Re: functional testing bug? (was Re: [Rails] create and functional testing)
> Strange. I''m not sure either. Does breakpointing from within the > controllers context yield any useful info? Eg: Does @params contain the > paste hash?@params returns nil> can you create and save a model with it?if I test manually in the unit test section all works -- Lawrence http://www.oluyede.org/blog
Lee O''Mara
2005-May-12 16:48 UTC
Re: functional testing bug? (was Re: [Rails] create and functional testing)
Lawrence Oluyede wrote:>>Strange. I''m not sure either. Does breakpointing from within the >>controllers context yield any useful info? Eg: Does @params contain the >>paste hash? > > @params returns nilThe fact that the controller isn''t seeing the given parameters is the root of your problem. I wonder if it''s not happy with your use of symbols as keys? -- Lee
Lawrence Oluyede
2005-May-12 20:04 UTC
Re: functional testing bug? (was Re: [Rails] create and functional testing)
2005/5/12, Lee O''Mara <lee-O8glSrxzjJo@public.gmane.org>:> The fact that the controller isn''t seeing the given parameters is the > root of your problem. I wonder if it''s not happy with your use of > symbols as keys?Tried also with plain strings but the results are the same :( -- Lawrence http://www.oluyede.org/blog