6

I'm trying to test for the presence of a form. I'm new to Rails.

My new.html.erb_spec.rb file's contents are:

require 'spec_helper'

describe "messages/new.html.erb" do
  it "should render the form" do
    render '/messages/new.html.erb'
    reponse.should have_form_putting_to(@message) 
    with_submit_button
  end
end

The view itself, new.html.erb, has the code:

<%= form_for(@message) do |f| %>
  <%= f.label :msg %> <br />
  <%= f.text_area :msg %>
  <%= f.submit "Submit" %>
<% end %>

When I run rspec, it fails as so:

1) messages/new.html.erb should render the form

 Failure/Error: render '/messages/new.html.erb'

   Missing partial /messages/new.html with {:handlers=>[:erb, :rjs,:builder,:rhtml, :rxml], :formats=>[:html,:text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]} in view paths "/Users/tristanmartin/whisperme/app/views"

   # ./spec/views/messages/new.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>'

Does anyone know what the problem is?

Thanks!

4
  • Could you try using have_form_posting_to instead of have_form_putting_to and see if your are still getting the error? Commented Dec 13, 2010 at 5:48
  • Try removing the leading slash from the pathname. Commented Dec 13, 2010 at 9:17
  • Thanks Raghu, I didn't notice I was using the wrong method. Still getting the error though. Commented Dec 14, 2010 at 0:46
  • I removed the leading slash from the pathname, and I still get the error. Commented Dec 14, 2010 at 0:46

3 Answers 3

7

don't give any argument to 'render'. try the following

require 'spec_helper'

describe "messages/new.html.erb" do
  it "should render the form" do
    render
    rendered.should contain('blablabla') 
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

kind of strange, but works (assigns no longer works -> use instance variables)
1

I experienced the same issue and I'll describe what I did to solve this, in case you haven't found the solution yet. I think the problem is rspec reports an misleading error here. The real error is something else.

I found this out by first changing the line to:

render :template => "messages/new.html.erb"

This allowed the actual error to surface. In my case, I was not setting up required variable(s), which I corrected.

Once you have set the assigns correctly, spec ran properly.

Then you may go back to either:

render "messages/new.html.erb"

or even just

render

both will work. Missing template issue should be disappeared. At least it did for me. :)

Comments

1

I ran into a similar issue, and was surprised to learn that Rspec does some impressive inference from the describe argument. For example:

require 'spec_helper'

describe "bills/payments/edit.html.erb" do
  it "Renders payment form" do
    assign(:payment, stub_model(Payment))
    render
  end
end

Due to some evolutionary controller / view names, this test originally had:

describe "bills/payment/edit.html.erb" do

that really messed everything up, and even if I set the :template after render, it was unable to find a referenced partial. Fixing up the path in the describe statement fixed it all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.