Rails: "missing partial" when calling 'render' in RSpec test
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=>开发者_如何学Python[: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!
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
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. :)
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.
精彩评论