Forum: RSpec render_template failure

0e48577b8626616d3deb19214de2ba5b?d=identicon&s=25 Daniel Lidström (Guest)
on 2010-09-06 16:39
(Received via mailing list)
Hello,

I am trying to run this integration test of my user controller within
a rails 3 application:

# spec/requests/users_spec.rb

require 'spec_helper'

describe "Users" do

    describe "success" do

      it "should make a new user" do
        lambda do
          visit signup_path
          fill_in "Name", :with => "Example User"
          fill_in "Email", :with => "user@example.com"
          fill_in "Password", :with => "foobar"
          fill_in "Confirmation", :with => "foobar"
          click_button "Sign up"
          page.should have_css("div.flash.success", :text =>
"Welcome")
          response.should render_template("user/show")
        end.should change(User, :count).by(1)
      end
    end
end

The test fails when render_template is being called:

 daniel@ubuntu /home/daniel/programming/bowling (signing-up) $ rspec
spec/requests/users_spec.rb -e "should make a new user"
Run filtered using {:full_description=>/(?-mix:should make a new
user)/}
F

Failures:
  1) Users signup success should make a new user
     Failure/Error: response.should render_template("user/show")
     @request must be an ActionDispatch::Request
     # ./spec/requests/users_spec.rb:34
     # ./spec/requests/users_spec.rb:26

Finished in 0.41306 seconds
1 example, 1 failure

I am using rspec-rails (2.0.0.beta.20) with capybara (0.3.9). What am
I missing here? For what it's worth I have verified the behaviour
manually: registering a new user does indeed take me to the show page.
Thanks in advance!

Daniel
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2010-09-06 16:39
(Received via mailing list)
On Sep 4, 2010, at 1:41 PM, Daniel Lidström wrote:

> Hello,
>
> I am trying to run this integration test of my user controller within
> a rails 3 application:
>
> # spec/requests/users_spec.rb
>
> require 'spec_helper'
>
> describe "Users" do
>
>    describe "success" do
>
>      it "should make a new user" do
>        lambda do
>          visit signup_path
>          fill_in "Name", :with => "Example User"
>          fill_in "Email", :with => "user@example.com"
>          fill_in "Password", :with => "foobar"
>          fill_in "Confirmation", :with => "foobar"
>          click_button "Sign up"
>          page.should have_css("div.flash.success", :text =>
> "Welcome")
>          response.should render_template("user/show")
>        end.should change(User, :count).by(1)
>      end
>    end
> end
>
> The test fails when render_template is being called:
>
> daniel@ubuntu /home/daniel/programming/bowling (signing-up) $ rspec
> spec/requests/users_spec.rb -e "should make a new user"
> Run filtered using {:full_description=>/(?-mix:should make a new
> user)/}
> F
>
> Failures:
>  1) Users signup success should make a new user
>     Failure/Error: response.should render_template("user/show")
>     @request must be an ActionDispatch::Request
>     # ./spec/requests/users_spec.rb:34
>     # ./spec/requests/users_spec.rb:26
>
> Finished in 0.41306 seconds
> 1 example, 1 failure
>
> I am using rspec-rails (2.0.0.beta.20) with capybara (0.3.9). What am
> I missing here? For what it's worth I have verified the behaviour
> manually: registering a new user does indeed take me to the show page.
> Thanks in advance!

The problem is that capybara doesn't assign anything to the @request
variable after visit, so it doesn't support any of the built-in rails
assertions that rely on @request, and render_template delegates to
assert_template.

I'd file a bug with capybara on this one.

Cheers,
David
0e48577b8626616d3deb19214de2ba5b?d=identicon&s=25 Daniel Lidström (Guest)
on 2010-09-06 16:39
(Received via mailing list)
On Sep 4, 8:41 pm, Daniel Lidström <dlidst...@gmail.com> wrote:
>           response.should render_template("user/show")

I noticed a mistake on the above line. However, the correct line (with
"users/show") also fails:

 daniel@ubuntu /home/daniel/programming/bowling (signing-up) $ ls app/
views/users/show.html.erb
app/views/users/show.html.erb
 daniel@ubuntu /home/daniel/programming/bowling (signing-up) $ rspec
spec/requests/users_spec.rb -e "should make a new user"
Run filtered using {:full_description=>/(?-mix:should make a new
user)/}
F

Failures:
  1) Users signup success should make a new user
     Failure/Error: response.should render_template("users/show")
     @request must be an ActionDispatch::Request
     # ./spec/requests/users_spec.rb:34
     # ./spec/requests/users_spec.rb:26

Finished in 0.27922 seconds
1 example, 1 failure
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2010-09-06 16:43
(Received via mailing list)
On Sep 4, 2010, at 2:26 PM, David Chelimsky wrote:

>
> On Sep 4, 2010, at 1:41 PM, Daniel Lidström wrote:
>
>> Hello,
>>
>> I am trying to run this integration test of my user controller within
>> a rails 3 application:
>>
>> # spec/requests/users_spec.rb
>>
>> require 'spec_helper'
>>
>> describe "Users" do
>>
>>   describe "success" do
>>
>>     it "should make a new user" do
>>       lambda do
>>         visit signup_path
>>         fill_in "Name", :with => "Example User"
>>         fill_in "Email", :with => "user@example.com"
>>         fill_in "Password", :with => "foobar"
>>         fill_in "Confirmation", :with => "foobar"
>>         click_button "Sign up"
>>         page.should have_css("div.flash.success", :text =>
>> "Welcome")
>>         response.should render_template("user/show")
>>       end.should change(User, :count).by(1)
>>     end
>>   end
>> end
>>
>> The test fails when render_template is being called:
>>
>> daniel@ubuntu /home/daniel/programming/bowling (signing-up) $ rspec
>> spec/requests/users_spec.rb -e "should make a new user"
>> Run filtered using {:full_description=>/(?-mix:should make a new
>> user)/}
>> F
>>
>> Failures:
>> 1) Users signup success should make a new user
>>    Failure/Error: response.should render_template("user/show")
>>    @request must be an ActionDispatch::Request
>>    # ./spec/requests/users_spec.rb:34
>>    # ./spec/requests/users_spec.rb:26
>>
>> Finished in 0.41306 seconds
>> 1 example, 1 failure
>>
>> I am using rspec-rails (2.0.0.beta.20) with capybara (0.3.9). What am
>> I missing here? For what it's worth I have verified the behaviour
>> manually: registering a new user does indeed take me to the show page.
>> Thanks in advance!
>
> The problem is that capybara doesn't assign anything to the @request variable after 
visit, so it doesn't support any of the built-in rails assertions that rely on @request, 
and render_template delegates to assert_template.

For more context, this works fine with the rails built-in get, post, etc
methods:

  get things_path
  response.should render_template("things/index")

... as well as webrat's visit method:

  visit things_path
  response.should render_template("things/index")

That's why I say it's really a capybara issue. Make sense?
E995148dd181ac82eaa41d0f8e7632d7?d=identicon&s=25 Toni Tuominen (Guest)
on 2010-09-06 16:44
(Received via mailing list)
Capybara is not a rails-specific solution so it doesn't know anything
about rails's rendering logic etc. I'm also fairly sure Jonas will say
that it's something that won't be implemented.

I think the key thing here is that rendering a template is a
controller level thing and Capybara is designed to be an acceptance
testing tool, hence you need to test results and not implementation
with it.

- Toni
E995148dd181ac82eaa41d0f8e7632d7?d=identicon&s=25 Toni Tuominen (Guest)
on 2010-09-07 09:44
(Received via mailing list)
>
>> Capybara is not a rails-specific solution so it doesn't know anything
>> about rails's rendering logic etc. I'm also fairly sure Jonas will say
>> that it's something that won't be implemented.
>
> Makes sense. So if we decided this was a bug, which lib should own this?

Personally I don't see why rspec needs to offer integration level
tools in controller specs at all. If people want to render views and
assert the rendered content I think they should use this gem for that
http://github.com/grimen/rspec_tag_matchers. Overall it's an excellent
tool for any case where you need to assert html. I've found it to be
of great value in helper specs.
This topic is locked and can not be replied to.