RSpec,Devise - 找不到有效的映射错误

时间:2016-05-26 23:36:06

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

我有一个Devise用户模型,在我的应用程序中,我有不同的角色,我通过我的用户模型中的枚举指定。当我运行admin角色的测试时,我在使用Devise运行RSpec测试时收到以下错误。我已尝试过类似问题的其他一些答案,但似乎没有任何效果。我希望你能指出我正确的方向。谢谢!

RuntimeError:
   Could not find a valid mapping for {:email=>"collin_cain@torpdoyle.info", :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

更新

我已经更新了管理类别控制器规范,特别是从sign_in用户到sign_in(管理员,用户)的Devise的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文件中包含了Devise Test Helpers,遗憾的是错误仍在继续。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您是否已在迁移中声明了角色,如

t.integer :role

因为这需要包含在迁移创建的结构中。

如果不是

添加该行,在您的数据库中删除该表并再次运行您的rake

答案 1 :(得分:0)

我能够解决自己的问题,并决定发布答案,希望它能帮助将来的某个人。

我没有在admin_categories_controller_spec文件中将用户角色设置为admin,而是在Users Factory中添加了嵌套工厂。

FactoryGirl.define do
  factory :user do
    email { Faker::Internet.email }
    password "12345678"
    password_confirmation "12345678"
    role 0

    factory :admin do
        role 2
    end
  end
end

并且测试结束如下:

describe 'GET #index' do
        context 'when admin signed in' do
            it 'renders the index template' do
                admin = create(:admin)
                sign_in admin
                get :index
                expect(response).to render_template(:index)
            end

            it 'assigns a list of categories' do
                admin = create(:admin)
                sign_in admin
                category = create(:category)
                get :index
                expect(assigns(:categories)).to eq([category])
            end
        end
    end