GregD
2010-Nov-29 20:58 UTC
Rails 3 rspec 2 rendering XML repsonse.should contain(<hash>.to_xml)
Hi all, Is there an issue with Rails 3 or rspec 2 that the repond_with({"success" => "test") where the response is not an xml text? I have almost converted an old Rails 2 app to Rails 3 and having an issue with the rspec tests and the response content for xml. It works for json, but not xml. Say I have something like this. SimpleController < ActionController::Base respond_to :html, :xml, :json def test_action @foo = { "success" => "test" } respond_with(@foo) do |format| format.html { render :text => "success"} end end end When I tested the xml rendering, I check the response contains (have_text in rails 2) for the content. In the past (rails 2 and rspec 1.x), I would test this as: response.should have_text("<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n<hash>\n <success>test</success>\n</hash>\n") I converted the ''have_text'' to ''contain'' to read: response.should contain("<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n<hash>\n <success>test</success>\n</hash>\n") This spec fails with on xml and not json: Failure/Error: response.should contain(success_text) expected the following element''s content to include "<?xml version="1.0" encoding="UTF-8"?> <hash> <success>xml</success> </hash> ": test It looks like, the response is only "test" which is the value of the hash. On json the response is as expected: {"success":"test"}. Sorry for the lame example, but I really did not want to go into big long explanation of the "real" test code. Basically, I test the response and it should contain valid xml or json based on the format. I even tried response.body and it fails. Is this a rspec issue? I did a pp of the response and the body does look like the expected result. Has anyone come across this? Thanks, GregD -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Chelimsky
2010-Nov-29 23:38 UTC
Re: Rails 3 rspec 2 rendering XML repsonse.should contain(<hash>.to_xml)
On Nov 29, 2:58 pm, GregD <gditr...-rDkNP/OoUoU@public.gmane.org> wrote:> Hi all, > > Is there an issue with Rails 3 or rspec 2 that the > repond_with({"success" => "test") where the response is not an xml > text? > > I have almost converted an old Rails 2 app to Rails 3 and having an > issue with the rspec tests and the response content for xml. It works > for json, but not xml. > > Say I have something like this. > > SimpleController < ActionController::Base > > respond_to :html, :xml, :json > > def test_action > @foo = { "success" => "test" } > respond_with(@foo) do |format| > format.html { render :text => "success"} > end > end > end > > When I tested the xml rendering, I check the response contains > (have_text in rails 2) for the content. In the past (rails 2 and > rspec 1.x), I would test this as: > response.should have_text("<?xml version=\"1.0\" encoding=\"UTF-8\"?> > \n<hash>\n <success>test</success>\n</hash>\n") > > I converted the ''have_text'' to ''contain'' to read: > response.should contain("<?xml version=\"1.0\" encoding=\"UTF-8\"?> > \n<hash>\n <success>test</success>\n</hash>\n")The contain matcher comes from either webrat or capybara, and is designed to specify content that is visible in a browser in an HTML page (i.e. not tags). I''d go with have_xpath instead, which is supported by both webrat and capybara, though with slightly different APIs for each (check their docs). HTH, David> > This spec fails with on xml and not json: > > Failure/Error: response.should contain(success_text) > expected the following element''s content to include "<?xml > version="1.0" encoding="UTF-8"?> > <hash> > <success>xml</success> > </hash> > ": > test > > It looks like, the response is only "test" which is the value of the > hash. > > On json the response is as expected: {"success":"test"}. > > Sorry for the lame example, but I really did not want to go into big > long explanation of the "real" test code. Basically, I test the > response and it should contain valid xml or json based on the format. > I even tried response.body and it fails. Is this a rspec issue? I > did a pp of the response and the body does look like the expected > result. > > Has anyone come across this? > > Thanks, > > GregD-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.