Search code examples
ruby-on-railsintegration-testingcapybaraactioncontroller

ActionController::RoutingError: No route matches [POST]


    require 'test_helper'

     class MyTest < ActionController::IntegrationTest

      test "view posts from login page" do
      visit("/logins/new")
      find_field('Username').set('abode')
      find_field('Password').set('efghi')
      click_link_or_button('Login')
      assert page.has_content?('Signed in!')
      end

      test "go to new user page" do
        visit("/logins/new")
        click_link("New user?")
        assert (current_path == "/users/new")
      end

    end

   Error:
test_view_posts_from_login_page(MyTest):
ActionController::RoutingError: No route matches [POST] "/logins/new"
    test/integration/view_posts_test.rb:12:in `block in <class:MyTest>'

It shows error for line 12. Is there a problem with button "Login" or the /logins/new path? The second test passes though so the path should be correct? What am I doing wrong?

Thanks!


Solution

  • It's really hard to tell what's going on here. In general if you are asking a question about a routing error you should post what's in your routes.rb file as well.

    That being said, I think whatever HTML gets generated for the form has it's action specified incorrectly.

    Example routes:

        tags GET    /tags(.:format)                {:action=>"index", :controller=>"tags"}
             POST   /tags(.:format)                {:action=>"create", :controller=>"tags"}
     new_tag GET    /tags/new(.:format)            {:action=>"new", :controller=>"tags"}
    edit_tag GET    /tags/:id/edit(.:format)       {:action=>"edit", :controller=>"tags"}
         tag GET    /tags/:id(.:format)            {:action=>"show", :controller=>"tags"}
             PUT    /tags/:id(.:format)            {:action=>"update", :controller=>"tags"}
             DELETE /tags/:id(.:format)            {:action=>"destroy", :controller=>"tags"}
    

    Notice where it says POST in the second column there. That means the action attribute for a new object form should be set to /tags. Having that there tells Rails to render the create action in the Tags controller. The same would be true for your login model.

    As far as what your form HTML code actually looks like, it probably looks something along the lines of:

    <form ... action="/logins/new" ...>...</form>
    

    When it should be

    <form ... action="/logins" ...>...</form>
    

    Hope this helps.