问题
I'm writing integration tests using rspec and capybara for a rails4 app that cover existing functionality (I was lazy when I first wrote the app). The following submit button works properly in the browser (Chrome) and redirects to a success page (as expected)
<div class="actions">
<%= button_to "RSVP Now", new_event_attendee_path(f), :id => "open-contacts-dialog-btn", :class => "inbox-sf-add-btn tip", :style => "background:lightgreen" %>
</div>
But, the following test fails,
describe "should show confirmation message after submitting the form" do
it "should go to success message when form submitted" do
click_link 'See more details'
click_button 'Sign Me Up!'
fill_in 'Email', with: "simon@example.com"
fill_in 'attendee_name', with: "Simon T"
fill_in 'attendee_phone', with: "1234567890"
fill_in 'attendee_num_guests', with: "1"
click_on 'RSVP Now'
page.should have_content("Awesome! You're signed up.")
end
end
with the below error:
Failure/Error: click_on 'RSVP Now'
ActionController::RoutingError:
No route matches [POST] "/events/%23%3CActionView::Helpers::FormBuilder:0x007ff9d7e003c0%3E/attendees/new"
My routes look like this: https://gist.github.com/srt32/6076872
I'm a bit lost because from the user perspective the action works but I can't get the test to replicate the behavior. Any help would be appreciated. Thanks!
回答1:
You want a regular submit tag.
If using a form helper block:
<%= f.submit "RSVP Now" %>
Or use the standalone tag helper:
<%= submit_tag "RSVP Now" %>
Read the documentation on how button_to
works. It is mainly used as links that should POST, such as actions that change data in some way. It is not for use with normal forms.
来源:https://stackoverflow.com/questions/17848926/button-to-form-submittal-working-in-browser-but-failing-rspec-capybara-test-with