Skip to content

Commit

Permalink
Merge pull request #7 from CloCkWeRX/faster_specs2
Browse files Browse the repository at this point in the history
Faster specs
  • Loading branch information
CloCkWeRX committed Dec 5, 2016
2 parents 3a46f40 + 4dc263a commit 6d8e508
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -1,3 +1,4 @@
--colour
--tty
--format progress
--profile
8 changes: 4 additions & 4 deletions app/models/users/user.rb
Expand Up @@ -160,12 +160,12 @@ def check_if_current_user
# Prevent deleting a user unless she has no artifacts left.
#----------------------------------------------------------------------------
def check_if_has_related_assets
artifacts = %w(Account Campaign Lead Contact Opportunity Comment Task).inject(0) do |sum, asset|
sum = %w(Account Campaign Lead Contact Opportunity Comment Task).detect do |asset|
klass = asset.constantize
sum += klass.assigned_to(self).count if asset != "Comment"
sum += klass.created_by(self).count

asset != "Comment" && klass.assigned_to(self).exists? || klass.created_by(self).exists?
end
artifacts == 0
sum == nil
end

# Define class methods
Expand Down
6 changes: 3 additions & 3 deletions spec/models/users/abilities/user_ability_spec.rb
Expand Up @@ -7,17 +7,17 @@ def all_actions

describe "User abilities" do
subject(:ability) { Ability.new(user) }
let(:subject_user) { create :user }
let(:subject_user) { build :user }

context "when site manager, I" do
let(:user) { create :user, admin: true }
let(:user) { build :user, admin: true }
all_actions.each do |do_action|
it { is_expected.to be_able_to(do_action, subject_user) }
end
end

context "when myself, I" do
let(:user) { create :user }
let(:user) { build :user }
let(:subject_user) { user }
all_actions.each do |do_action|
it { is_expected.to be_able_to(do_action, subject_user) }
Expand Down
31 changes: 18 additions & 13 deletions spec/models/users/user_spec.rb
Expand Up @@ -128,48 +128,53 @@

context "scopes" do
describe "have_assigned_opportunities" do
before :each do
before do
@user1 = FactoryGirl.create(:user)
FactoryGirl.create(:opportunity, assignee: @user1, stage: 'analysis')
FactoryGirl.create(:opportunity, assignee: @user1, stage: 'analysis', account: nil, campaign: nil, user: nil)

@user2 = FactoryGirl.create(:user)

@user3 = FactoryGirl.create(:user)
FactoryGirl.create(:opportunity, assignee: @user3, stage: 'won')
FactoryGirl.create(:opportunity, assignee: @user3, stage: 'won', account: nil, campaign: nil, user: nil)

@user4 = FactoryGirl.create(:user)
FactoryGirl.create(:opportunity, assignee: @user4, stage: 'lost')
FactoryGirl.create(:opportunity, assignee: @user4, stage: 'lost', account: nil, campaign: nil, user: nil)

@result = User.have_assigned_opportunities
end

it "includes users with assigned opportunities" do
expect(User.have_assigned_opportunities).to include(@user1)
expect(@result).to include(@user1)
end

it "excludes users without any assigned opportunities" do
expect(User.have_assigned_opportunities).not_to include(@user2)
expect(@result).not_to include(@user2)
end

it "excludes users with opportunities that have been won or lost" do
expect(User.have_assigned_opportunities).not_to include(@user3)
expect(User.have_assigned_opportunities).not_to include(@user4)
expect(@result).not_to include(@user3)
expect(@result).not_to include(@user4)
end
end
end

context "instance methods" do
describe "assigned_opportunities" do
before :each do
before do
@user = FactoryGirl.create(:user)
@opportunity1 = FactoryGirl.create(:opportunity, assignee: @user)
@opportunity2 = FactoryGirl.create(:opportunity, assignee: FactoryGirl.create(:user))

@opportunity1 = FactoryGirl.create(:opportunity, assignee: @user, account: nil, campaign: nil, user: nil)
@opportunity2 = FactoryGirl.create(:opportunity, assignee: FactoryGirl.create(:user), account: nil, campaign: nil, user: nil)

@result = @user.assigned_opportunities
end

it "includes opportunities assigned to user" do
expect(@user.assigned_opportunities).to include(@opportunity1)
expect(@result).to include(@opportunity1)
end

it "does not include opportunities assigned to another user" do
expect(@user.assigned_opportunities).not_to include(@opportunity2)
expect(@result).not_to include(@opportunity2)
end
end
end
Expand Down

0 comments on commit 6d8e508

Please sign in to comment.