问题描述
我正在使用devise,rolify和cancan。我也使用rspec与FactoryGirl进行测试。现在我正在进行一些测试,我想为这些测试定义不同角色的用户。这是我目前猜测如何使用FactoryGirl:FactoryGirl.define do
factory:user do
名称'测试用户'
电子邮件'[email protected]'
密码'请'
password_confirmation'请'
#如果使用Devise可确认模块
confirm_at Time.now
工厂:admin do
self.has_role:admin
end
工厂:策展人做
自我.has_role:策展人
end
工厂:super_admin do
self.has_role:super_admin
end
end
end
这里有一些我的测试似乎没有正确写入:
require'spec_helper' p>
描述Pagesdo
subject {page}
before do
@ newpage = FactoryGirl.create(:page)
@ newpage.save
end
上下文'n没有登录'do
it_behaves_like'不管理'
end
上下文'登录'do
上下文'作为用户'做
before do
@user = FactoryGirl.create(:user)
sign_in @user
end
it_behaves_like'not admin'
end
上下文'作为策展人'做
之前做
@curator = FactoryGirl.create(:策展人)
sign_in @curator
结束
it_behaves_like'not admin'
end
context'as admin'do
before do
@admin = FactoryGirl.create(:admin)
sign_in @admin
end
it_behaves_like'admin'
end
上下文'作为超级管理员'做
之前做
@ super_a dmin = FactoryGirl.create(:super_admin)
sign_in @super_admin
end
it_behaves_like'admin'
end
end
end
当我运行规格我这些是我的错误:
1)以管理员身份登录的页面的行为与管理员可以显示页面
失败/错误:无法从backtrace找到匹配的行
NoMethodError:
未定义的方法`has_role ='for#< User:0x007f883384d178>
共享示例组:从./spec/requests/pages_spec.rb:41
#./spec/requests/pages_spec.rb:37:in中调用的admin块(4级) ;顶部(必需)>'
2)以策展人身份登录的页面不像管理员可以显示页面
失败/错误:无法从backtrace找到匹配的行
ArgumentError :
工厂未注册:策展人
共享示例组:从./spec/requests/pages_spec.rb:32
#./spec/requests/pages_spec.rb调用的not admin 28:在`block(4个级别)< top(必需)>'
3)以超级管理员身份登录的页面像管理员可以显示页面
失败/错误:无法从backtrace找到匹配行
ArgumentError:
工厂未注册:super_admin
共享示例组:从./spec/requests/pages_spec.rb:50
# ./spec/requests/pages_spec.rb:46:in`block(4级)in< top(required)>'
我宁愿在(:create)之后使用FactoryGirls 创建角色(另请参阅Rolify的问题)。
此外,方法 has_role?用于检查,如果用户具有特定的角色,要设置一个特定角色,你应该使用 add_role 方法。
FactoryGirl.define do
factory:user do
name'Test User'
email'[email protected]'
password'please'
password_confirmation'please'
#如果使用Devise Confirmable模块,则需要
confirmation_at Time.now
工厂:admin do
after(:create){ |用户| user.add_role(:admin)}
end
factory:curator do
after(:create){| user | user.add_role(:curator)}
end
工厂:super_admin do
after(:create){| user | user.add_role(:super_admin)}
end
end
end
I am using devise, rolify and cancan. I'm also using rspec with FactoryGirl for testing. Right now I'm working on some tests and I want to define users with different roles for those tests. Here is my current guess for how to do that using FactoryGirl:
FactoryGirl.define do factory :user do name 'Test User' email '[email protected]' password 'please' password_confirmation 'please' # required if the Devise Confirmable module is used confirmed_at Time.now factory :admin do self.has_role :admin end factory :curator do self.has_role :curator end factory :super_admin do self.has_role :super_admin end end end
Here are some of my tests which don't seem to be written correctly:require 'spec_helper'
describe "Pages" do subject { page } before do @newpage = FactoryGirl.create(:page) @newpage.save end context 'not logged in' do it_behaves_like 'not admin' end context 'logged in' do context 'as user' do before do @user = FactoryGirl.create(:user) sign_in @user end it_behaves_like 'not admin' end context 'as curator' do before do @curator = FactoryGirl.create(:curator) sign_in @curator end it_behaves_like 'not admin' end context 'as admin' do before do @admin = FactoryGirl.create(:admin) sign_in @admin end it_behaves_like 'admin' end context 'as super admin' do before do @super_admin = FactoryGirl.create(:super_admin) sign_in @super_admin end it_behaves_like 'admin' end end end
When I run the specs I these are my errors:
1) Pages logged in as admin behaves like admin can show page Failure/Error: Unable to find matching line from backtrace NoMethodError: undefined method `has_role=' for #<User:0x007f883384d178> Shared Example Group: "admin" called from ./spec/requests/pages_spec.rb:41 # ./spec/requests/pages_spec.rb:37:in `block (4 levels) in <top (required)>' 2) Pages logged in as curator behaves like not admin can show page Failure/Error: Unable to find matching line from backtrace ArgumentError: Factory not registered: curator Shared Example Group: "not admin" called from ./spec/requests/pages_spec.rb:32 # ./spec/requests/pages_spec.rb:28:in `block (4 levels) in <top (required)>' 3) Pages logged in as super admin behaves like admin can show page Failure/Error: Unable to find matching line from backtrace ArgumentError: Factory not registered: super_admin Shared Example Group: "admin" called from ./spec/requests/pages_spec.rb:50 # ./spec/requests/pages_spec.rb:46:in `block (4 levels) in <top (required)>'
I would rather use FactoryGirls after(:create) callback to create roles (also see this issue for Rolify).
Furthermore the method has_role? is used to check if a user has a specific role, to set a specific role you should use the add_role method.
FactoryGirl.define do factory :user do name 'Test User' email '[email protected]' password 'please' password_confirmation 'please' # required if the Devise Confirmable module is used confirmed_at Time.now factory :admin do after(:create) {|user| user.add_role(:admin)} end factory :curator do after(:create) {|user| user.add_role(:curator)} end factory :super_admin do after(:create) {|user| user.add_role(:super_admin)} end end end
这篇关于在FactoryGirl定义中通过rolify设置角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!