“Could not find a valid mapping for #” only on second and successive tests

后端 未结 9 1915
花落未央
花落未央 2020-12-28 13:28

I\'m trying to write a request test that asserts that the proper links appear on the application layout depending in whether a user is logged in or out. FWIW, I\'m using Dev

相关标签:
9条回答
  • 2020-12-28 14:10

    I had this error in my routes file because I had an API endpoint that I wanted to allow users to reset their passwords from, but I wanted the users to actually do the password change on the web view, which wasn't namespaced under /api

    This is how I fixed the routes to make it work:

    CoolApp::Application.routes.draw do
      namespace :api, defaults: { format: :json } do
        devise_scope :users do
          post 'users/passwords', to: 'passwords#create'
        end
    
        resources :users, only: [:create, :update]
      end
    
      devise_for :users
    
      root to: 'high_voltage/pages#show', id: 'home'
    end
    
    0 讨论(0)
  • 2020-12-28 14:10

    I had this happen to me before when I signed in as a test user on one of my apps and had made some test uploads and test posts. I then deleted that test user and when I tried to sign up again with the same test user I was displayed the message "Could not find a valid mapping for nil". What I did to solve it was to delete all of the test uploads and test post that I had made as that test user. I then tried to sign up again and it worked. By that way a faster and easier way to delete stuff is by using db browser for sqlite.

    0 讨论(0)
  • 2020-12-28 14:11

    In my case this was a problem with Devis's confirm! method. So instead of doing this (Minitest::Test code):

    setup do
      @admin = create(:admin).confirm!
    end
    

    I have done this:

    setup do
      @admin = create(:admin)
      @admin.confirm!
    end
    

    And it worked :)

    0 讨论(0)
  • 2020-12-28 14:12

    Add this line to your routes.rb

    # Devise routes
    devise_for :users # Or the name of your custom model
    
    0 讨论(0)
  • 2020-12-28 14:15

    Thanks to : http://blog.thefrontiergroup.com.au/2011/03/reloading-factory-girl-factories-in-the-rails-3-console/

    "Devise uses a mapping between classes and routes, so when a factory built object comes through to Devise after a console reload, or a class redefinition then it will fail."

    Put this in an initializer or application.rb

    ActionDispatch::Callbacks.after do
      # Reload the factories
      return unless (Rails.env.development? || Rails.env.test?)
    
      unless FactoryGirl.factories.blank? # first init will load factories, this should only run on subsequent reloads
        FactoryGirl.factories.clear
        FactoryGirl.find_definitions
      end
    end
    
    0 讨论(0)
  • 2020-12-28 14:17

    Halfnelson's solution worked for me too, but since I'm using the Fabrication gem instead of FactoryGirl, I needed to make some adjustments. Here's the code I droped into a initializer:

    if Rails.env.development? || Rails.env.test?
      ActionDispatch::Callbacks.after do
        Fabrication.clear_definitions
        Rails.logger.debug 'Reloading fabricators'
      end
    end
    
    0 讨论(0)
提交回复
热议问题