ruby on rails 3 - RSpec tests with devise: could not find valid mapping -
i'm trying run controller specs devise 1.3.4. (and factory girl) followed instructions in git wiki project. able log in user using login_user method created in macro, login_admin fails following error:
... sign_in factory.create(:admin) not find valid mapping #<user id: 2023, email: "admin1@gmail.com", .... >
factory:
factory.define :user |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 |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) @request.env["devise.mapping"] = devise.mappings[:user] #it should map user because admin not model of own. produces same result either way. @admin = factory.create(:admin) sign_in @admin end end def login_user before(:each) @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 set cache_classes = false, isn't ideal use spork , don't want have restart after changing model.
any help?
i have in routes:
devise_for :accounts, :controllers => {:confirmations => "confirmations"} put "confirm_account", :to => "confirmations#confirm_account" "login" => "devise/sessions#new", :as => :login delete "logout" => "devise/sessions#destroy", :as => :logout "register" => "devise/registrations#new", :as => :register end
so in spec/support/controller_macros.rb needed change from:
def login_account before(:each) @request.env["devise.mapping"] = devise.mappings[:account] @account = factory.create(:account) sign_in(@account) end end
to
def login_account before(:each) @request.env["devise.mapping"] = devise.mappings[:account] @account = factory.create(:account) sign_in(:account, @account) end end
note sign_in(scope, resource)
i hope helps.
Comments
Post a Comment