“无法找到 #<User ...> 的有效映射”仅在第二次和后续测试中

问题描述 投票:0回答:9

我正在尝试编写一个请求测试,断言根据用户是否登录或注销,应用程序布局上会显示正确的链接。 FWIW,我正在使用 Devise 进行身份验证。

这是我的规格:

require 'spec_helper'
require 'devise/test_helpers'

describe "Layout Links" do
  context "the home page" do
    context "session controls" do
      context "for an authenticated user" do
        before do
          # I know these should all operate in isolation, but I
          # want to make sure the user is explicitly logged out
          visit destroy_user_session_path

          @user = Factory(:user, :password => "Asd123", :password_confirmation => "Asd123")
          @user.confirm!

          # I tried adding this per the Devise wiki, but no change
          @request.env["devise.mapping"] = Devise.mappings[:user]

          # Now log a new user in
          visit new_user_session_path
          fill_in "Email",    :with => @user.email
          fill_in "Password", :with => "Asd123"
          click_button "Sign in"
          get '/'
        end

        it "should not have a link to the sign in page" do
          response.should_not have_selector(
            '#session a',
            :href => new_user_session_path
          )
        end

        it "should not have a link to registration page" do
          response.should_not have_selector(
            '#session a',
            :href => new_user_registration_path
          )
        end

        it "should have a link to the edit profile page" do
          response.should have_selector(
            '#session a',
            :content => "My Profile",
            :href => edit_user_registration_path
          )
        end

        it "should have a link to sign out page" do
          response.should have_selector(
            '#session a',
            :content => "Logout",
            :href => destroy_user_session_path
          )
        end
      end # context "for an authenticated user"
    end # context "session controls"
  end
end

第一个测试通过,但最后三个测试都失败并出现错误

Failure/Error: @user = Factory(:user, :password => "Asd123", :password_confirmation => "Asd123")
  RuntimeError:
    Could not find a valid mapping for #<User id: xxx, ...>

我已经在 Devise wiki、Google 群组和搜索结果中搜索了原因,但我发现的都是未解答的问题或设置的建议

config.include Devise::TestHelpers, :type => :controller
但这仅适用于控制器测试,不适用于请求测试。


更新

我已经做了一些更多的故障排除,但我无法弄清楚最终触发问题的原因。看看下面的代码。

首先,对于一些上下文,这里是用户工厂声明。它在单元测试中运行良好。

# spec/factories.rb
Factory.define :user do |f|
  f.email { Faker::Internet.email }
  f.email_confirmation { |f| f.email }
  f.password "AbcD3fG"
  f.password_confirmation "AbcD3fG"
  f.remember_me { (Random.new.rand(0..1) == 1) ? true : false }
end

现在,考虑以下集成测试

# spec/requests/user_links_spec.rb
require "spec_helper"

describe "User Links" do
  before(:each) do
    # This doesn't trigger the problem
    # @user = nil

    # This doesn't trigger the problem
    # @user = User.new

    # This doesn't trigger the problem
    # @user = User.create(
    #   :email => "[email protected]", 
    #   :email_confirmation => "[email protected]", 
    #   :password => "asdf1234", 
    #   :password_confirmation => "asdf1234"
    # )

    # This doesn't trigger the problem
    # @user = User.new
    # @user.email = Faker::Internet.email
    # @user.email_confirmation = @user.email
    # @user.password = "AbcD3fG"
    # @user.password_confirmation = "AbcD3fG"
    # @user.remember_me = (Random.new.rand(0..1) == 1) ? true : false
    # @user.save!

    # This triggers the problem!
    @user = Factory(:user)

    # This doesn't trigger the same problem, but it raises a ActiveRecord::AssociationTypeMismatch error instead. Still no idea why. It was working fine before in other request tests.
    # @user = Factory(:brand)
  end

  context "when using `@user = Factory(:user)` in the setup: " do
    2.times do |i|
      it "this should pass on the 1st iteration, but not the 2nd (iteration ##{i+1})" do
        # This doesn't trigger an error
        true.should_not eql(false)
      end

      it "this should pass on the 1st iteration, but trigger the error that causes all successive test cases to fail (iteration ##{i+1})" do
        # Every test case after this will be borken!
        get '/'
      end

      it "this will fail on all iterations (iteration ##{i+1})" do
        # This will now trigger an error
        true.should_not eql(false)
      end
    end
  end
end

如果我们注释掉或用其他任何内容(或什么都不替换)替换

get '/'
位,测试都会运行良好。

所以,我不知道这是一个factory_girl问题(我倾向于怀疑它,因为我可以在其他地方使用用户工厂而不会出现问题)还是一个Devise问题(在我的应用程序中设置该gem后我开始收到这些错误,但我也只有一个其他请求测试,它工作得很好,但现在得到了 AssociationTypeMismatch 错误;相关性≠因果关系...)或 RSpec 问题或其他一些奇怪的边缘情况宝石冲突。

devise integration-testing rspec2 factory-bot
9个回答
26
投票

将此行添加到您的routes.rb

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

9
投票

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

“Devise 使用类和路由之间的映射,因此当工厂构建的对象在控制台重新加载或类重新定义后到达 Devise 时,它将失败。”

将其放入初始化程序或 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

9
投票

对于未来的读者:我收到了同样的错误,但原因不同。

我们的应用程序有多个用户模型,其中一个模型派生于另一个模型

为了论证:

class SimpleUser

class User < SimpleUser

在我的控制器中,我使用了对 SimpleUser(父类)的引用,但 Devise 配置为使用 User(子类)。

在调用 Devise::Mapping.find_scope 期间!它确实是.is_a?与对象引用和配置的类进行比较。

由于我的引用是 SimpleUser,并且配置的类是 User,因此 .is_a?失败,因为比较询问父类是否 is_a?子类,这总是错误的。

希望对其他人有帮助。


4
投票

我的路由文件中出现此错误,因为我有一个 API 端点,我希望用户可以从中重置密码,但我希望用户实际上在 Web 视图上更改密码,而 Web 视图并未在

 下进行命名空间/api

这就是我修复路线以使其正常工作的方法:

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

2
投票

Halfnelson 的解决方案也对我有用,但由于我使用的是 Fabrication gem 而不是 FactoryGirl,我需要进行一些调整。这是我放入初始化程序中的代码:

if Rails.env.development? || Rails.env.test?
  ActionDispatch::Callbacks.after do
    Fabrication.clear_definitions
    Rails.logger.debug 'Reloading fabricators'
  end
end

1
投票

我知道这是一个旧线程,但我想为遇到此问题的其他人提供答案。不知道我在哪里读到这篇文章,但支持在另一个论坛上发布此内容的人。

总而言之,设计测试助手不能很好地与集成测试配合使用。

因此,从规范本身中删除所有对 Devise::TestHelpers 的引用(有些人使用 include,其他人使用 require 'devise/testhelpers')。

然后在spec_helper文件中添加:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

1
投票

就我而言,这是 Devis 的

confirm!
方法的问题。因此,不要这样做(
Minitest::Test
代码):

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

我已经做到了:

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

而且成功了:)


1
投票

以防万一其他人遇到这个问题,出于与我相同的原因 - 在更改一些配置文件后,我在基本上所有测试中都遇到了同样的问题 - 事实证明这是由于

RAILS_ENV
设置为
development 
当我不小心运行测试时。在添加特定于测试的 Rails 初始化程序之前可能值得检查:-)


0
投票

我以前就遇到过这种情况,当时我以测试用户身份登录我的一个应用程序并进行了一些测试上传和测试帖子。然后我删除了该测试用户,当我尝试使用同一测试用户再次注册时,我显示消息“无法找到 nil 的有效映射”。我解决这个问题的方法是删除我作为测试用户所做的所有测试上传和测试帖子。然后我尝试再次注册,结果成功了。通过这种方式,一种更快、更简单的删除内容的方法是使用 db browser for sqlite

© www.soinside.com 2019 - 2024. All rights reserved.