"Could not find a valid mapping for #<User ...>" only on second and successive tests

Add this line to your routes.rb

# Devise routes
devise_for :users # Or the name of your custom model

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

For future readers: I received same error, but for a different reason.

Our app had multiple user models where one derived from the other

For arguments sake:

class SimpleUser

class User < SimpleUser

In my controller I used a reference to SimpleUser (the parent class), but Devise was configured to use User (the child class).

During an invocation of Devise::Mapping.find_scope! it does .is_a? comparison against the object reference and the configured class.

Since my reference was a SimpleUser, and the configured class was User, the .is_a? fails because the comparison was asking if the Parent class is_a? Child class, which is always false.

Hope that helps someone else.