Created
March 17, 2012 22:37
-
-
Save lauer/2065954 to your computer and use it in GitHub Desktop.
Problems with controller.action_name in view tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= render :partial => 'form' %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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'
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
Can you use a
Factory
instead ofstub_model
?