gpt4 book ai didi

ruby-on-rails - CanCan::AccessDenied with factory_girl 和 cancan,如何正确编写工厂?

转载 作者:行者123 更新时间:2023-11-28 20:37:56 26 4
gpt4 key购买 nike

第三天我很痛苦,我不明白为什么我没有通过下一个测试:

 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),有很多固定数据。

我尝试将我的测试环境从 fixtures 迁移到 factory_girl。所以我对此很陌生。

现在我正在使用:

  • cancancan + 设计
  • factory_girl + 测试用例

我的文章 Controller :

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

我的文章 Controller 测试:

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你需要创造

Controller tests (Test::Unit)

To sign in as admin for a given test case, just do:

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

Note: If you are using the confirmable module, you should set a confirmed_at date inside the Factory or call confirm! before sign_in.

Here is the basics to prepare inside your Factory:

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

关于ruby-on-rails - CanCan::AccessDenied with factory_girl 和 cancan,如何正确编写工厂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46847919/

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