本文介绍了设计的RSpec测试:找不到有效的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行控制器规格与设计1.3.4。 (和工厂女孩)
我按照git wiki中的说明进行了项目。我可以使用在宏中创建的login_user方法作为用户登录,但是login_admin失败,出现以下错误:

  ... 
sign_in Factory.create(:admin)

找不到#< User id:2023,email:[email protected]的有效映射。 ..>

工厂:

 code> Factory.define:user do | f | 
f.sequence(:username){| n | user#{n}}
f.sequence(:email){| n | user#{n}@gmail.com}
f.email_confirmation {| fac | fac.email}
f.passworda12345Den123
f.password_confirmationa12345Den123
#f.admin 0
end

Factory.define: admin,:class =>用户做| f |
f.sequence(:username){| n | admin#{n}}
f.sequence(:email){| n | admin#{n}@gmail.com}
f.email_confirmation {| fac | fac.email}
f.passworda12345Den123
f.password_confirmationa12345Den123
f.admin 1
end

控制器宏模块:

  module ControllerMacros 
def login_admin
before(:each)do
@ request.env [devise.mapping] = Devise.mappings [:user] #it应该映射到用户,因为管理员不是自己的模型。它可以产生相同的结果。
@admin = Factory.create(:admin)
sign_in @admin
end
end

def login_user
before(:each) do
@ request.env [devise.mapping] = Devise.mappings [:user]
@user = Factory.create(:user)
sign_in @user
end
end
end

路线

  devise_for:users 
devise_for:admins,:class_name => 'user'

一个解决方案是设置cache_classes = false,但是这不太理想,因为我使用更改模型时不想重新启动它。



任何帮助?

解决方案

我在路线上有这样的事情:

  devise_for:accounts,,controllers => ; {:confirmations => confirmations} do 
putconfirm_account,:to => confirmations#confirm_account
getlogin=> devise / sessions#new,:as => :login
deletelogout=> devise / sessions#destroy,:as => :logout
getregister=> devise / registrations#new,:as => :注册
end

所以在我的spec / support / controller_macros.rb我需要更改from:

  def login_account 
before(:each)do
@ request.env [devise。映射] = Devise.mappings [:account]
@account = Factory.create(:account)
sign_in(@account)
end
end

to

  def login_account 
before(:each)do
@ request.env [devise.mapping] = Devise.mappings [:account]
@account = Factory.create(:account)
sign_in(:account,@account)
end
end

注意 sign_in(范围,资源)



我希望这有助于。


I'm trying to run controller specs with devise 1.3.4. (and factory girl)I followed the instructions in the git wiki for the project. I am able to log in as a user using the login_user method created in the macro, but login_admin fails with the following error:

...
sign_in Factory.create(:admin)

Could not find a valid mapping for #<User id: 2023, email: "[email protected]", .... >

Factory:

Factory.define :user do |f|
  f.sequence(:username) {|n| "user#{n}"}
  f.sequence(:email) {|n| "user#{n}@gmail.com"}
  f.email_confirmation {|fac| fac.email }
  f.password "a12345Den123"
  f.password_confirmation "a12345Den123"
#  f.admin 0
end

Factory.define :admin, :class => User do |f|
  f.sequence(:username) {|n| "admin#{n}"}
  f.sequence(:email) {|n| "admin#{n}@gmail.com"}
  f.email_confirmation {|fac| fac.email }
  f.password "a12345Den123"
  f.password_confirmation "a12345Den123"
  f.admin 1
end

Controller macros module:

module ControllerMacros
  def login_admin
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]  #it should map to user because admin is not a model of its own.  It produces the same result either way.
      @admin = Factory.create(:admin)
      sign_in @admin
    end
  end

  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @user = Factory.create(:user)
      sign_in @user
    end
  end
end

routes

devise_for :users
devise_for :admins, :class_name => 'User'

One solution is to set cache_classes = false, however that isn't ideal as I use spork and don't want to have to restart it after changing a model.

Any help?

解决方案

I have something like this in my routes:

  devise_for :accounts, :controllers => {:confirmations => "confirmations"} do
    put "confirm_account", :to => "confirmations#confirm_account"
    get "login" => "devise/sessions#new", :as => :login
    delete "logout" => "devise/sessions#destroy", :as => :logout
    get "register" => "devise/registrations#new", :as => :register
  end

so in my spec/support/controller_macros.rb I needed to change from:

  def login_account
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:account]
      @account = Factory.create(:account)
      sign_in(@account)
    end
  end

to

  def login_account
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:account]
      @account = Factory.create(:account)
      sign_in(:account, @account)
    end
  end

note the sign_in(scope, resource)

I hope this helps.

这篇关于设计的RSpec测试:找不到有效的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:16