gpt4 book ai didi

ruby-on-rails-3 - 使用设计 : could not find valid mapping 进行 RSpec 测试

转载 作者:行者123 更新时间:2023-12-04 23:30:19 34 4
gpt4 key购买 nike

我正在尝试使用设计 1.3.4 运行 Controller 规范。 (和工厂女孩)
我按照项目的 git wiki 中的说明进行操作。我可以使用在宏中创建的 login_user 方法以用户身份登录,但 login_admin 失败并出现以下错误:

...
sign_in Factory.create(:admin)

Could not find a valid mapping for #<User id: 2023, email: "admin1@gmail.com", .... >

工厂:
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 宏模块:
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

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

一种解决方案是设置 cache_classes = false,但这并不理想,因为我使用 spork 并且不想在更改模型后重新启动它。

有什么帮助吗?

最佳答案

我的 route 有这样的东西:

  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

所以在我的 spec/support/controller_macros.rb 我需要改变:
  def login_account
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:account]
@account = Factory.create(:account)
sign_in(@account)
end
end


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

注意 登录(范围,资源)

我希望这有帮助。

关于ruby-on-rails-3 - 使用设计 : could not find valid mapping 进行 RSpec 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5966971/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com