Skip to content

Instantly share code, notes, and snippets.

@lauer
Created March 17, 2012 22:37
Show Gist options
  • Save lauer/2065954 to your computer and use it in GitHub Desktop.
Save lauer/2065954 to your computer and use it in GitHub Desktop.
Problems with controller.action_name in view tests
<%= form_for @car, :html => { :class => 'form-horizontal' } do |f| %>
<fieldset>
<legend><%=controller.action_name.capitalize %> Car</legend>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', cars_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>
Running: spec/views/cars/new.html.erb_spec.rb
Running tests with args ["--color", "--failure-exit-code", "2", "--format", "progress", "--format", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--require", "/opt/local/lib/ruby/gems/1.8/gems/guard-rspec-0.6.0/lib/guard/rspec/formatters/notification_rspec.rb", "spec/views/cars/new.html.erb_spec.rb"]...
F
Failures:
1) cars/new renders new car form
Failure/Error: render
ActionView::Template::Error:
undefined method `capitalize' for nil:NilClass
# ./app/views/cars/_form.html.erb:3:in `_app_views_cars__form_html_erb___1022961548_2520415460'
# ./app/views/cars/_form.html.erb:1:in `_app_views_cars__form_html_erb___1022961548_2520415460'
# ./app/views/cars/new.html.erb:1:in `_app_views_cars_new_html_erb___977315666_2520559140'
# ./spec/views/cars/new.html.erb_spec.rb:11
Finished in 0.12481 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/views/cars/new.html.erb_spec.rb:10 # cars/new renders new car form
Done.
<%= render :partial => 'form' %>
require 'spec_helper'
describe "cars/new" do
before(:each) do
assign(:car, stub_model(Car,
:name => "MyString"
).as_new_record)
controller.action_name = 'new'
end
it "renders new car form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => cars_path, :method => "post" do
assert_select "input#car_name", :name => "car[name]"
end
end
end
@fr33z3
Copy link

fr33z3 commented Mar 17, 2012

Can you use a Factory instead of stub_model?

@lauer
Copy link
Author

lauer commented Mar 18, 2012

I could yes, that is maybe next step. - but the solution was to add a stub for the controller.action_name

controller.action_name = 'new'

@redconfetti
Copy link

Thanks for posting this. I just started working with RSpec, and the scaffold generated code had this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment