我有一個設計用戶模型,在我的應用程序中我有不同的角色,我通過我的用戶模型中的枚舉指定。當我運行管理員角色的測試時,在使用Devise運行RSpec測試時,我收到以下錯誤。我嘗試了類似問題的其他答案,但似乎沒有任何工作。我希望你能指出我正確的方向。謝謝!RSpec,設計 - 找不到有效的映射錯誤
RuntimeError:
Could not find a valid mapping for {:email=>"[email protected]", :password=>"12345678", :password_confirmation=>"12345678", :role=>2}
這裏是用戶模型:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :comments
enum role: [:member, :moderator, :admin]
before_save :set_default_role
def set_default_role
self.role ||= 0
end
end
用戶工廠:
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
password "12345678"
password_confirmation "12345678"
role 0
end
end
類別控制器規格
require 'rails_helper'
RSpec.describe Admin::CategoriesController, type: :controller do
it 'should redirect to sign in path for non signed users' do
get :index
expect(response).to redirect_to(new_user_session_path)
end
it 'should redirect to root path for non admin users' do
user = create(:user)
sign_in user
get :index
expect(response).to redirect_to(root_path)
end
describe 'GET #index' do
context 'when admin signed in' do
it 'renders the index template' do
admin = attributes_for(:user, role: 2)
sign_in admin
get :index
expect(response).to render_template(:index)
end
it 'assigns a list of categories' do
admin = attributes_for(:user, role: 2)
sign_in admin
category = create(:category)
expect(assigns(:categories)).to eq([category])
end
end
end
end
和路由文件
Rails.application.routes.draw do
devise_for :users
namespace :admin do
get '', to: 'dashboard#index', as: '/'
resources :categories
end
resources :topics do
resources :comments, only: :create
end
resources :categories do
resources :topics
end
root 'categories#index'
end
我也加入了用戶模式
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "role"
t.string "image"
end
UPDATE:
我已經更新了管理類控制器規範,從sign_in用戶sign_in(特別設計的sign_in方法:管理員,用戶),如下所示。
describe 'GET #index' do
context 'when admin signed in' do
it 'renders the index template' do
user = create(:user)
user.role = 2
sign_in(:admin, user)
get :index
expect(response).to render_template(:index)
end
...
現在我收到以下錯誤
1) Admin::CategoriesController GET #index when admin signed in renders the index template
Failure/Error: expect(response).to render_template(:index)
expecting <"index"> but was a redirect to <http://test.host/users/sign_in>
出於某種原因,管理員沒有被登錄,我已經包含在rails_helper.rb文件制定測試助手,不幸的是,錯誤繼續。任何幫助將不勝感激。
嗨@AAnkudovich,是的,我原本宣佈在遷移的作用,我已經編輯我最初的問題,包括用戶模式。 –