【问题标题】:Rspec and Devise: Could not find a valid mapping for #<UserRspec 和设计:找不到 #<User 的有效映射
【发布时间】:2011-08-24 21:28:33
【问题描述】:

使用 RSpec 和 Devise,我遇到了映射错误。

  1) BusinessesController GET index sets @new_vacation
     Failure/Error: sign_in @user
     RuntimeError:
       Could not find a valid mapping for #<User id: 8009, email: "persons3@cycle7.com", encrypted_password: "$2a$10$joN9Vy6spNAe4uh7W9fcOOdoeSJ5dr/nBwpmlqcv6EYe...", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2011-04-01 17:05:03", updated_at: "2011-04-01 17:05:03", business_id: 7214, confirmation_token: nil, confirmed_at: "2011-08-24 21:23:51", confirmation_sent_at: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, admin: true, password_salt: nil>
     # ./spec/controllers/businesses_controller_spec.rb:10:in `block (2 levels) in <top (required)>'

看起来答案可能在这里RSpec tests with devise: could not find valid mapping,但我无法得到它。

任何想法,我真的很难过!谢谢。


routes.rb

  constraints(SubdomainRoute) do

    # SIGN IN
    devise_for :accounts, :controllers => {
                                                      :sessions => "users/accounts/sessions",
                                                      :confirmations =>  "users/accounts/confirmations" },
                                                      :class_name => 'Admin' do
      get "accounts/sign_in", :to => "users/accounts/sessions#new"

      get "accounts/sign_up", :to => "users/devise/registrations#new"
      get "accounts/signedup", :to => "users/devise/confirmations#signedup", :as => "signedup_registration"
    end
end

admin.rb

class Admin < User
  validates_associated :business
end

用户.rb

class User < ActiveRecord::Base
  belongs_to :business
  accepts_nested_attributes_for :business

  # TODO get lockable working -- failed_attempts is incrementing in db
  devise :database_authenticatable, :registerable, :confirmable, :lockable,
         :recoverable, :rememberable, :trackable, :validatable
end

工厂.rb

FactoryGirl.define do

  Factory.define :business do |b|
  end

  Factory.define :business_main, :parent => :business do |b|
    b.business_name "Ann's Salon"
    b.address_line1 "123 Main Street"
    b.address_line2 ""
    b.city "Portland"
    b.state "Oregon"
    b.zip "97211"
  end

  factory :admin do
    confirmed_at Time.now

    factory :admin_one do
      association :business, :factory => :business_one
      email
      password 'please'
      admin true
    end
  end
end

business_controller_spec.rb

require 'spec_helper'

describe BusinessesController do

  before (:each) do
    @business = Factory(:business_one)
    @user     = User.find_by_business_id(@business.id)
    sign_in @user
  end

  describe "GET index" do

    let(:describe_action) { get :index }

    it "sets @new_vacation" do
      BusinessVacation.should_receive(:new)
      get :index
    end
  end

end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rspec devise


    【解决方案1】:

    我的路由文件中出现此错误,因为我有一个 API 端点,我希望允许用户从中重置密码,但我希望用户实际在未命名空间的 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
    

    【讨论】:

    • 对我来说刚刚添加了 'devise_for :users' 并且有效。谢谢@Neal。
    • 我想给这个一百万个赞。根本无法弄清楚缺少什么!
    【解决方案2】:

    首先确保您在路线中调用了“devise_for”,在模型中调用了“devise”。我建议您阅读 README 和 wiki 中的文档以获取更多信息。

    如果是,试试这个:

    将此代码放入您的 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    
    

    这是因为 Devise 使用类和路由之间的映射,所以当一个工厂构建的对象在控制台重新加载或类重新定义后通过 Devise 时,它​​将失败。这在开发和测试环境中很常见。

    我在这里找到它并为我工作得很好- http://blog.thefrontiergroup.com.au/2011/03/reloading-factory-girl-factories-in-the-rails-3-console/

    【讨论】:

    • 在 Rails 控制台中执行 reload! 会导致同样的问题弄乱设计映射。完全重启控制台修复了它。
    【解决方案3】:

    您可能需要检查您的代码中不同位置的多个devise_for :users 声明。在我的案例中,这就是导致这种异常的原因,因为它肯定会让 Devise 感到困惑。

    【讨论】:

      【解决方案4】:

      我的问题与此代码无关。我最近添加了一个前置过滤器以始终重定向到 www。

      redirect_to request.protocol + "www." + request.host_with_port + request.fullpath if !/^www/.match(request.host)
      

      我仍然不完全确定为什么这会导致我的问题,但确实如此。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多