简体   繁体   中英

How to divide a big rspec file into smaller parts

So, in my project I am using cancancan for authorization. For writing ability spec we are maintaining in ability_spec.rb. From last few years the files size has grown enormousely. Now it has around 3000 lines which is pretty much and the main problem which we are facing is we aren't able to run the whole file in one shot. If we try to do it our system freezes. Inorder to resolve this problem I came up with approach to divide the file into multiple files. Since, rspec follows naming convention is so I am bit perplexed How to proceed on it.

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

Ability file path - app/models/ability.rb

Ability spec file path - spec/models/ability_spec.rb

So, far I tried to create an ability_folder inside spec/models and I divided the specs based on the roles in their respective files. Something like below

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

When I try to run this it didn't gave me any errors and it ran within a second. Also, I have tried to introduce an error deliberatory still it didn't fail. So, my questions are

1) How to solve this kind of problem when the file size is enormous

2) If the above approach is correct then why it is not working for me

3) Will there be any performance improvement if I divide the file

  1. Splitting a big spec file sounds like a good idea. It's a nightmare to navigate in gigantic spec file. However the problem moves to maintaining multiple spec files (which is probably still less evil).
  2. As far as I know it didn't work for you because of Test database issue. You solved it, right?
  3. You may see a small performance improvement if you run parallel specs. Instead of running 1 long spec in 1 thread it will run smaller specs in parallel. If you don't run it in parallel it won't give you any performance improvement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM