From c9c1f76e002d899dd6765c4c1630697cc5068f27 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 28 Aug 2012 01:42:28 -0400 Subject: [PATCH] All specs and features currently passing with FactoryGirl --- features/support/env.rb | 6 +- spec/factories.rb | 107 ++++++++++++++++++ spec/factories_spec.rb | 83 ++++++++++++++ spec/helpers/gitlab_flavored_markdown_spec.rb | 4 +- spec/models/system_hook_spec.rb | 11 +- spec/spec_helper.rb | 1 + spec/support/monkeypatch.rb | 21 ---- spec/support/stubbed_repository.rb | 60 ++++++++++ 8 files changed, 262 insertions(+), 31 deletions(-) create mode 100644 spec/factories.rb create mode 100644 spec/factories_spec.rb delete mode 100644 spec/support/monkeypatch.rb create mode 100644 spec/support/stubbed_repository.rb diff --git a/features/support/env.rb b/features/support/env.rb index da6c1b70168..5357815201a 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -8,8 +8,8 @@ require 'webmock/cucumber' WebMock.allow_net_connect! -require Rails.root.join 'spec/support/monkeypatch' require Rails.root.join 'spec/support/gitolite_stub' +require Rails.root.join 'spec/support/stubbed_repository' require Rails.root.join 'spec/support/login_helpers' require Rails.root.join 'spec/support/valid_commit' @@ -52,6 +52,8 @@ require 'cucumber/rspec/doubles' include GitoliteStub -Before do +Before do stub_gitolite! end + +World(FactoryGirl::Syntax::Methods) diff --git a/spec/factories.rb b/spec/factories.rb new file mode 100644 index 00000000000..eb3882a6bee --- /dev/null +++ b/spec/factories.rb @@ -0,0 +1,107 @@ +# Backwards compatibility with the old method +def Factory(type, *args) + FactoryGirl.create(type, *args) +end + +module Factory + def self.create(type, *args) + FactoryGirl.create(type, *args) + end + + def self.new(type, *args) + FactoryGirl.build(type, *args) + end +end + +FactoryGirl.define do + sequence :sentence, aliases: [:title, :content] do + Faker::Lorem.sentence + end + + sequence(:url) { Faker::Internet.uri('http') } + + factory :user, aliases: [:author, :assignee, :owner] do + email { Faker::Internet.email } + name { Faker::Name.name } + password "123456" + password_confirmation "123456" + + trait :admin do + admin true + end + + factory :admin, traits: [:admin] + end + + factory :project do + sequence(:name) { |n| "project#{n}" } + path { name } + code { name } + owner + end + + factory :users_project do + user + project + end + + factory :issue do + title + author + project + + trait :closed do + closed true + end + + factory :closed_issue, traits: [:closed] + end + + factory :merge_request do + title + author + project + source_branch "master" + target_branch "stable" + end + + factory :note do + project + note "Note" + end + + factory :event do + end + + factory :key do + title + key { File.read(File.join(Rails.root, "db", "pkey.example")) } + end + + factory :milestone do + title + project + end + + factory :system_hook do + url + end + + factory :project_hook do + url + end + + factory :wiki do + title + content + user + end + + factory :snippet do + project + author + title + content + file_name { Faker::Lorem.sentence } + end +end diff --git a/spec/factories_spec.rb b/spec/factories_spec.rb new file mode 100644 index 00000000000..126a5b2309f --- /dev/null +++ b/spec/factories_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' + +describe "Factories" do + describe 'User' do + it "builds a valid instance" do + build(:user).should be_valid + end + + it "builds a valid admin instance" do + build(:admin).should be_valid + end + end + + describe 'Project' do + it "builds a valid instance" do + build(:project).should be_valid + end + end + + describe 'Issue' do + it "builds a valid instance" do + build(:issue).should be_valid + end + + it "builds a valid closed instance" do + build(:closed_issue).should be_valid + end + end + + describe 'MergeRequest' do + it "builds a valid instance" do + build(:merge_request).should be_valid + end + end + + describe 'Note' do + it "builds a valid instance" do + build(:note).should be_valid + end + end + + describe 'Event' do + it "builds a valid instance" do + build(:event).should be_valid + end + end + + describe 'Key' do + it "builds a valid instance" do + build(:key).should be_valid + end + end + + describe 'Milestone' do + it "builds a valid instance" do + build(:milestone).should be_valid + end + end + + describe 'SystemHook' do + it "builds a valid instance" do + build(:system_hook).should be_valid + end + end + + describe 'ProjectHook' do + it "builds a valid instance" do + build(:project_hook).should be_valid + end + end + + describe 'Wiki' do + it "builds a valid instance" do + build(:wiki).should be_valid + end + end + + describe 'Snippet' do + it "builds a valid instance" do + build(:snippet).should be_valid + end + end +end diff --git a/spec/helpers/gitlab_flavored_markdown_spec.rb b/spec/helpers/gitlab_flavored_markdown_spec.rb index e147cb39375..28bd46ecb99 100644 --- a/spec/helpers/gitlab_flavored_markdown_spec.rb +++ b/spec/helpers/gitlab_flavored_markdown_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe GitlabMarkdownHelper do before do - @project = Project.find_by_path("gitlabhq") || Factory(:project) + @project = Factory(:project) @commit = @project.repo.commits.first.parents.first @commit = CommitDecorator.decorate(Commit.new(@commit)) @other_project = Factory :project, path: "OtherPath", code: "OtherCode" @@ -157,7 +157,7 @@ describe GitlabMarkdownHelper do gfm("Let @#{user.name} fix the *mess* in #{@commit.id}").should == "Let #{link_to "@#{user.name}", project_team_member_path(@project, member), class: "gfm gfm-team_member "} fix the *mess* in #{link_to @commit.id, project_commit_path(@project, id: @commit.id), title: "Commit: #{@commit.author_name} - #{@commit.title}", class: "gfm gfm-commit "}" end - it "should not trip over other stuff", focus: true do + it "should not trip over other stuff" do gfm("_Please_ *stop* 'helping' and all the other b*$#%' you do.").should == "_Please_ *stop* 'helping' and all the other b*$#%' you do." end diff --git a/spec/models/system_hook_spec.rb b/spec/models/system_hook_spec.rb index 56d76ed08cf..fe2a5836fe7 100644 --- a/spec/models/system_hook_spec.rb +++ b/spec/models/system_hook_spec.rb @@ -10,13 +10,12 @@ describe SystemHook do end it "project_create hook" do - user = Factory :user with_resque do - project = Factory :project_without_owner, owner: user + project = Factory :project end WebMock.should have_requested(:post, @system_hook.url).with(body: /project_create/).once end - + it "project_destroy hook" do project = Factory :project with_resque do @@ -31,7 +30,7 @@ describe SystemHook do end WebMock.should have_requested(:post, @system_hook.url).with(body: /user_create/).once end - + it "user_destroy hook" do user = Factory :user with_resque do @@ -39,7 +38,7 @@ describe SystemHook do end WebMock.should have_requested(:post, @system_hook.url).with(body: /user_destroy/).once end - + it "project_create hook" do user = Factory :user project = Factory :project @@ -48,7 +47,7 @@ describe SystemHook do end WebMock.should have_requested(:post, @system_hook.url).with(body: /user_add_to_team/).once end - + it "project_destroy hook" do user = Factory :user project = Factory :project diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fc5ba14659a..d381b3f1e2e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,6 +27,7 @@ RSpec.configure do |config| config.include LoginHelpers, type: :request config.include GitoliteStub + config.include FactoryGirl::Syntax::Methods # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false diff --git a/spec/support/monkeypatch.rb b/spec/support/monkeypatch.rb deleted file mode 100644 index 04bbb6fb680..00000000000 --- a/spec/support/monkeypatch.rb +++ /dev/null @@ -1,21 +0,0 @@ -# Stubbing Project <-> git host path -# create project using Factory only -class Project - def path_to_repo - File.join(Rails.root, "tmp", "tests", path) - end - - def satellite - @satellite ||= FakeSatellite.new - end -end - -class FakeSatellite - def exists? - true - end - - def create - true - end -end diff --git a/spec/support/stubbed_repository.rb b/spec/support/stubbed_repository.rb new file mode 100644 index 00000000000..fa6c4c9a9b6 --- /dev/null +++ b/spec/support/stubbed_repository.rb @@ -0,0 +1,60 @@ +# Stubs out all Git repository access done by models so that specs can run +# against fake repositories without Grit complaining that they don't exist. +module StubbedRepository + extend ActiveSupport::Concern + + included do + # If a class defines the method we want to stub directly, rather than + # inheriting it from a module (as is the case in UsersProject), that method + # will overwrite our stub, so use alias_method to ensure it's our stub + # getting called. + + alias_method :update_repository, :fake_update_repository + alias_method :destroy_repository, :fake_destroy_repository + alias_method :repository_delete_key, :fake_repository_delete_key + alias_method :path_to_repo, :fake_path_to_repo + alias_method :satellite, :fake_satellite + end + + def fake_update_repository + true + end + + def fake_destroy_repository + true + end + + def fake_repository_delete_key + true + end + + def fake_path_to_repo + if new_record? + # There are a couple Project specs that expect the Project's path to be + # in the returned path, so let's patronize them. + File.join(Rails.root, 'tmp', 'tests', path) + else + # For everything else, just give it the path to one of our real seeded + # repos. + File.join(Rails.root, 'tmp', 'tests', 'gitlabhq_1') + end + end + + def fake_satellite + FakeSatellite.new + end + + class FakeSatellite + def exists? + true + end + + def create + true + end + end +end + +[Project, Key, ProtectedBranch, UsersProject].each do |c| + c.send(:include, StubbedRepository) +end