[LRUG] Rspec sees different output from Rails application

Simon Coffey simon at tribesports.com
Sat Jul 16 05:55:13 PDT 2011


Hi John,

By default, rspec will not render your views in a controller spec, so
that controller logic can be tested in isolation from template code. I
suspect the output you're seeing is stub content created by rspec
rather than your app. If you want your controller test to render the
views using your own templates, add the following line:

describe SomeController do
  render_views
  ...
end

This is in rspec-2, which I assume you're using; in rspec-1 it's
"integrate_views". More info here:
https://github.com/rspec/rspec-rails

All the best,
Simon

On 16 July 2011 13:31, John Winters <john at sinodun.org.uk> wrote:
> I'm trying out Rspec with Rails 3.1pre to see whether I can use it for
> testing, using a book called "Ruby on Rails 3 Tutorial" for guidance.
>
> I seem to have got it installed and running but I'm getting odd results.
>
> The first suggested test is simply to ensure a page can be retrieved, and
> that works fine.
>
> The second suggest test is to check its title is correct.  The spec for
> this reads:
>
>  describe "GET 'about'" do
>    it "should be successful" do
>      get 'about'
>      response.should be_success
>    end
>
>    it "should have the right title" do
>      get 'about'
>      response.should have_selector("title",
>        :content => "Ruby on Rails Tutorial Sample App | About")
>    end
>  end
>
> and this second test fails.  The trouble is, the complaint message from
> rspec shows only part of the output it's checking (and not the relevant
> bit), but the part it shows doesn't match what you get when you access the
> page with a normal web browser.
>
> With a normal web browser, the source of the retrieved page starts like
> this:
>
> <!DOCTYPE html>
> <html>
>  <head>
>    <title>Ruby on Rails Tutorial Sample App | About</title>
>  </head>
>  ...
>
> and yet the complaint from Rspec looks like this:
>
>  3) PagesController GET 'about' should have the right title
>     Failure/Error: response.should have_selector("title",
>       expected following output to contain a <title>Ruby on Rails
> Tutorial Sample App | About</title> tag:
>       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
> "http://www.w3.org/TR/REC-html40/loose.dtd">
>     # ./spec/controllers/pages_controller_spec.rb:39:in `block (3 levels)
> in <top (required)>'
>
> Note the completely different start to the page.  I've searched the whole
> application source and nowhere can I find anything which would generate
> this output.
>
> Anyone any idea where Rspec is getting this from?
>
> TIA,
> John
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>



-- 
Simon Coffey
Developer
TRIBESPORTS

e: simon at tribesports.com
m: 07960 004 857
p: Unit 104, Cremer Business Centre, 37 Cremer Street, London, E2 8HD



More information about the Chat mailing list