From a82977c64843aa177498f1a715121ae6e38bc323 Mon Sep 17 00:00:00 2001
From: randx <dmitriy.zaporozhets@gmail.com>
Date: Sat, 15 Sep 2012 01:00:59 +0300
Subject: [PATCH] A bit of test refactoring

---
 Guardfile                                     |  2 +-
 app/observers/users_project_observer.rb       |  5 +--
 spec/models/event_spec.rb                     | 41 +++++++++----------
 spec/observers/users_project_observer_spec.rb | 41 ++++++++++---------
 .../requests/gitlab_flavored_markdown_spec.rb |  1 +
 spec/support/gitolite_stub.rb                 | 34 ++-------------
 6 files changed, 49 insertions(+), 75 deletions(-)

diff --git a/Guardfile b/Guardfile
index 0eea2ad924..50a10af9a3 100644
--- a/Guardfile
+++ b/Guardfile
@@ -1,7 +1,7 @@
 # A sample Guardfile
 # More info at https://github.com/guard/guard#readme
 
-guard 'rspec', :version => 2, :all_on_start => false do
+guard 'rspec', :version => 2, :all_on_start => false, :all_after_pass => false do
   watch(%r{^spec/.+_spec\.rb$})
   watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
   watch('spec/spec_helper.rb')  { "spec" }
diff --git a/app/observers/users_project_observer.rb b/app/observers/users_project_observer.rb
index b2cea9f279..0512e60671 100644
--- a/app/observers/users_project_observer.rb
+++ b/app/observers/users_project_observer.rb
@@ -15,10 +15,9 @@ class UsersProjectObserver < ActiveRecord::Observer
 
   def after_destroy(users_project)
     Event.create(
-      project_id: users_project.project.id, 
-      action: Event::Left, 
+      project_id: users_project.project.id,
+      action: Event::Left,
       author_id: users_project.user.id
     )
   end
-
 end
diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb
index ee022e959e..5cb68761b2 100644
--- a/spec/models/event_spec.rb
+++ b/spec/models/event_spec.rb
@@ -14,12 +14,12 @@ describe Event do
     it { should respond_to(:commits) }
   end
 
-  describe "Push event" do 
-    before do 
+  describe "Push event" do
+    before do
       project = Factory :project
       @user = project.owner
 
-      data = { 
+      data = {
         before: "0000000000000000000000000000000000000000",
         after: "0220c11b9a3e6c69dc8fd35321254ca9a7b98f7e",
         ref: "refs/heads/master",
@@ -50,25 +50,24 @@ describe Event do
     it { @event.author.should == @user }
   end
 
-  describe "Joined project team" do
-    let(:project) {Factory.create :project}
-    let(:new_user) {Factory.create :user}
-    it "should create event" do
-      UsersProject.observers.enable :users_project_observer
-      expect{
-        UsersProject.bulk_import(project, [new_user.id], UsersProject::DEVELOPER)
-      }.to change{Event.count}.by(1)
+  describe 'Team events' do
+    let(:user_project) { stub.as_null_object }
+    let(:observer) { UsersProjectObserver.instance }
+
+    before {
+      Event.should_receive :create
+    }
+
+    describe "Joined project team" do
+      it "should create event" do
+        observer.after_create user_project
+      end
     end
-  end
-  describe "Left project team" do
-    let(:project) {Factory.create :project}
-    let(:new_user) {Factory.create :user}
-    it "should create event" do
-      UsersProject.bulk_import(project, [new_user.id], UsersProject::DEVELOPER)
-      UsersProject.observers.enable :users_project_observer
-      expect{
-        UsersProject.bulk_delete(project, [new_user.id])
-      }.to change{Event.count}.by(1)
+
+    describe "Left project team" do
+      it "should create event" do
+        observer.after_destroy user_project
+      end
     end
   end
 end
diff --git a/spec/observers/users_project_observer_spec.rb b/spec/observers/users_project_observer_spec.rb
index f38d98620a..07d71da8c7 100644
--- a/spec/observers/users_project_observer_spec.rb
+++ b/spec/observers/users_project_observer_spec.rb
@@ -1,34 +1,32 @@
 require 'spec_helper'
 
 describe UsersProjectObserver do
-  let(:user) { Factory.create :user }
-  let(:project) { Factory.create(:project, 
-                                 code: "Fuu", 
-                                 path: "Fuu" ) }
-  let(:users_project) { Factory.create(:users_project,
-                                        project: project,
-                                        user: user )}
+  let(:users_project) { stub.as_null_object }
   subject { UsersProjectObserver.instance }
 
   describe "#after_create" do
     it "should called when UsersProject created" do
       subject.should_receive(:after_create)
+
       UsersProject.observers.enable :users_project_observer do
-        Factory.create(:users_project, 
-                       project: project, 
-                       user: user)
+        create(:users_project)
       end
     end
+
     it "should send email to user" do
-      Notify.should_receive(:project_access_granted_email).with(users_project.id).and_return(double(deliver: true))
+      Event.stub(:create => true)
+      Notify.should_receive(:project_access_granted_email).and_return(stub(deliver: true))
+
       subject.after_create(users_project)
     end
+
     it "should create new event" do
       Event.should_receive(:create).with(
-        project_id: users_project.project.id, 
-        action: Event::Joined, 
+        project_id: users_project.project.id,
+        action: Event::Joined,
         author_id: users_project.user.id
       )
+
       subject.after_create(users_project)
     end
   end
@@ -36,29 +34,32 @@ describe UsersProjectObserver do
   describe "#after_update" do
     it "should called when UsersProject updated" do
       subject.should_receive(:after_update)
+
       UsersProject.observers.enable :users_project_observer do
-        users_project.update_attribute(:project_access, 40)
+        create(:users_project).update_attribute(:project_access, 40)
       end
     end
+
     it "should send email to user" do
       Notify.should_receive(:project_access_granted_email).with(users_project.id).and_return(double(deliver: true))
+
       subject.after_update(users_project)
     end
   end
+
   describe "#after_destroy" do
     it "should called when UsersProject destroyed" do
       subject.should_receive(:after_destroy)
+
       UsersProject.observers.enable :users_project_observer do
-        UsersProject.bulk_delete(
-          users_project.project,
-          [users_project.user.id]
-        )
+        create(:users_project).destroy
       end
     end
+
     it "should create new event" do
       Event.should_receive(:create).with(
-        project_id: users_project.project.id, 
-        action: Event::Left, 
+        project_id: users_project.project.id,
+        action: Event::Left,
         author_id: users_project.user.id
       )
       subject.after_destroy(users_project)
diff --git a/spec/requests/gitlab_flavored_markdown_spec.rb b/spec/requests/gitlab_flavored_markdown_spec.rb
index 1076e90c42..68d354b7bc 100644
--- a/spec/requests/gitlab_flavored_markdown_spec.rb
+++ b/spec/requests/gitlab_flavored_markdown_spec.rb
@@ -25,6 +25,7 @@ describe "Gitlab Flavored Markdown" do
     @tag_name = "gfm-test-tag"
     r.git.native(:tag, {}, @tag_name, commit.id)
   end
+
   after do
     # delete test branch and tag
     project.repo.git.native(:branch, {D: true}, @branch_name)
diff --git a/spec/support/gitolite_stub.rb b/spec/support/gitolite_stub.rb
index 037b09cd55..574bb5a12a 100644
--- a/spec/support/gitolite_stub.rb
+++ b/spec/support/gitolite_stub.rb
@@ -5,42 +5,16 @@ module GitoliteStub
   end
 
   def stub_gitolite_admin
-    gitolite_repo = mock(
-      clean_permissions: true,
-      add_permission: true
-    )
-
-    gitolite_config = mock(
-      add_repo: true,
-      get_repo: gitolite_repo,
-      has_repo?: true
-    )
-
-    gitolite_admin = double(
-      'Gitolite::GitoliteAdmin',
-      config: gitolite_config,
-      save: true,
-    )
+    gitolite_admin = double('Gitolite::GitoliteAdmin')
+    gitolite_admin.as_null_object
 
     Gitolite::GitoliteAdmin.stub(new: gitolite_admin)
-
   end
 
   def stub_gitlab_gitolite
     gitolite_config = double('Gitlab::GitoliteConfig')
-    gitolite_config.stub(
-      apply: ->() { yield(self) },
-      write_key: true,
-      rm_key: true,
-      update_projects: true,
-      update_project: true,
-      update_project!: true,
-      destroy_project: true,
-      destroy_project!: true,
-      admin_all_repo: true,
-      admin_all_repo!: true,
-
-    )
+    gitolite_config.stub(apply: ->() { yield(self) })
+    gitolite_config.as_null_object
 
     Gitlab::GitoliteConfig.stub(new: gitolite_config)
   end
-- 
2.30.9