簡體   English   中英

CanCan::AccessDenied with factory_girl 和 cancan,如何正確寫工廠?

[英]CanCan::AccessDenied with factory_girl and cancan, How correctly to write the factory?

我已經第三天痛苦了,我不明白為什么我沒有通過下一個測試:

 4) Error:
ArticlesControllerTest#test_should_get_index_if_admin:
CanCan::AccessDenied: You are not authorized to access this page.
    test/controllers/articles_controller_test.rb:22:in `block in <class:ArticlesControllerTest>'

我究竟做錯了什么? 請幫幫我!

我有舊的應用程序(rails 4.2),有很多夾具數據。

我嘗試將我的測試環境從夾具遷移到 factory_girl。 所以我是新手。

現在我正在使用:

  • 康康+設計
  • factory_girl + 測試用例

我的文章控制器:

class ArticlesController < ApplicationController
  load_and_authorize_resource
  before_filter :authenticate_user!, except: [:show]

  def index
    @articles = Article.paginate(page: params[:page], per_page: 10).includes(:translations)
  end
end

能力.rb:

Class Ability
 include CanCan::Ability

 def initialize(user)
   user ||= User.new

   # Everybody
   can :show, [Article]

   if user.admin?
     can :manage, Article
   end
 end
end

我的工廠 article.rb 很簡單:

FactoryGirl.define do
 factory :article do
   content "MyText"

   factory :one_article
   factory :two_article
 end
end

我的工廠 user.rb 也很簡單:

FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "user#{n}@mail.ru" }
    password "password"
    password_confirmation "password"
    after(:create) {|u| u.roles_mask = 4}
    profile

    factory :valid_admin do
      first_name "Administrator"
      last_name "Administrator"
      association :profile, factory: :admin_profile
      after(:create) {|u| u.roles_mask = 2}
    end
  end
end

我的文章控制器測試:

require 'test_helper'

class ArticlesControllerTest < ActionController::TestCase
  include Devise::Test::ControllerHelpers

  setup do
    @article = create(:one_article)
    @admin   = create(:valid_admin)
  end

  test 'should get index if admin' do
    sign_in @admin

    ability = Ability.new(@admin)
    assert ability.can? :index, Article

    get :index
    assert_response :success
    assert_not_nil assigns(:articles)
  end
end

撬的信息:

[1] pry(#<ArticlesControllerTest>)> sign_in @admin
=> [[20709], "9BET5RWNuJPrGHUFi86d"]
[2] pry(#<ArticlesControllerTest>)> ability = Ability.new(@admin)
=> #<Ability:0x0000000c3c5ff8
 @rules=
  [#<CanCan::Rule:0x0000000c3c5f80
    @actions=[:show],
    @base_behavior=true,
    @block=nil,
.............<<Many lines>> ..............
[3] pry(#<ArticlesControllerTest>)> assert ability.can? :index, Article
=> true
[4] pry(#<ArticlesControllerTest>)> get :index
CanCan::AccessDenied: You are not authorized to access this page.
from /home/da/.rvm/gems/ruby-2.2.6@wenya/gems/cancancan-1.16.0/lib/cancan/ability.rb:217:in `authorize!'

在此先感謝您的幫助!

這是設計指南,您需要創建一個方法來以管理員身份登錄用戶。 這是您需要創建的login_method

控制器測試(測試::單元)

要以管理員身份登錄給定的測試用例,只需執行以下操作:

class SomeControllerTest < ActionController::TestCase
  # For Devise >= 4.1.1
  include Devise::Test::ControllerHelpers
  # Use the following instead if you are on Devise <= 4.1.0
  # include Devise::TestHelpers

  def setup
    @request.env["devise.mapping"] = Devise.mappings[:admin]
    sign_in FactoryGirl.create(:admin)
  end
end

注意:如果您使用的是confirmable 模塊,您應該在Factory 中設置confirmed_at 日期或致電confirm! 登錄前。

以下是在工廠內准備的基礎知識:

FactoryGirl.define do
  factory :account do
    email { Faker::Internet.email }
    password "password"
    password_confirmation "password"
    confirmed_at Date.today
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM