因此,在我的项目中,我使用cancancan进行授权。对于编写能力规范,我们将其保留在capability_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中创建一个bility_folder,并根据各自文件中的角色划分了spec。如下所示
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)如果分割文件,性能会有任何改善吗?>
因此,在我的项目中,我使用cancancan进行授权。对于编写能力规范,我们将其保留在capability_spec.rb中。从最近几年开始,文件大小急剧增加。现在它周围有...