【发布时间】:2020-04-28 17:26:01
【问题描述】:
所以,在我的项目中,我使用 cancancan 进行授权。对于编写能力规范,我们在ability_spec.rb 中维护。从过去几年开始,文件大小已经大大增加。现在它有大约 3000 行,这几乎是我们面临的主要问题是我们无法一次运行整个文件。如果我们尝试这样做,我们的系统就会冻结。为了解决这个问题,我想出了将文件分成多个文件的方法。因为,rspec 遵循命名约定,所以我有点困惑如何继续它。
class Ability
include CanCan::Ability
attr_accessor :feature_factory
def initialize(user, session = nil)
alias_action :read, :update, :to => :read_write
user ||= User.new # guest user (not logged in)
can :read, Abc
can :manage, :file
can :view, :login
if user.anonymous_user?
## a lot of abiities
end
if user.mum_user?
## a lot of abiities
end
if user.tsm_user?
## a lot of abiities
end
#Similarly we have a lot of roles and abilities
end
require 'spec_helper'
require "cancan/matchers"
describe Ability do
describe "#anonymous_user" do
## a lot of specs for testing
end
describe "#tsm_user" do
## a lot of specs for testing
end
describe "#mum_user" do
## a lot of specs for testing
end
# similarly for other user roles
end
能力文件路径 - app/models/ability.rb
能力规格文件路径 - spec/models/ability_spec.rb
所以,到目前为止,我尝试在 spec/models 中创建一个能力文件夹,并根据各自文件中的角色划分规范。类似下面的东西
spec/models/ability/anonymous_user_spec.rb
require 'spec_helper'
require "cancan/matchers"
describe Ability do
describe "#anonymous_user" do
## a lot of specs for testing
end
end
spec/models/ability/tsm_user_spec.rb
require 'spec_helper'
require "cancan/matchers"
describe Ability do
describe "#tsm_user" do
## a lot of specs for testing
end
end
spec/models/ability/mum_user_spec.rb
require 'spec_helper'
require "cancan/matchers"
describe Ability do
describe "#mum_user" do
## a lot of specs for testing
end
end
当我尝试运行它时,它没有给我任何错误,它在一秒钟内运行。此外,我试图引入一个错误审议,但它并没有失败。所以,我的问题是
1) 文件很大时如何解决此类问题
2)如果上述方法是正确的,那么为什么它对我不起作用
3) 分割文件会不会有性能提升
【问题讨论】:
-
一个类的 3000 行规范是疯狂的。是时候转移到 Pundit 了,因为您在康康坎语时代已经长大了。
标签: ruby-on-rails ruby rspec ruby-on-rails-5.2 cancancan