Use double instead of OpenStruct in the Issue/PR formatters specs

parent 3225f78f
...@@ -2,13 +2,14 @@ require 'spec_helper' ...@@ -2,13 +2,14 @@ require 'spec_helper'
describe Gitlab::GithubImport::IssueFormatter, lib: true do describe Gitlab::GithubImport::IssueFormatter, lib: true do
let!(:project) { create(:project, namespace: create(:namespace, path: 'octocat')) } let!(:project) { create(:project, namespace: create(:namespace, path: 'octocat')) }
let(:octocat) { OpenStruct.new(id: 123456, login: 'octocat') } let(:octocat) { double(id: 123456, login: 'octocat') }
let(:created_at) { DateTime.strptime('2011-01-26T19:01:12Z') } let(:created_at) { DateTime.strptime('2011-01-26T19:01:12Z') }
let(:updated_at) { DateTime.strptime('2011-01-27T19:01:12Z') } let(:updated_at) { DateTime.strptime('2011-01-27T19:01:12Z') }
let(:base_data) do let(:base_data) do
{ {
number: 1347, number: 1347,
milestone: nil,
state: 'open', state: 'open',
title: 'Found a bug', title: 'Found a bug',
body: "I'm having a problem with this.", body: "I'm having a problem with this.",
...@@ -26,7 +27,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do ...@@ -26,7 +27,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do
describe '#attributes' do describe '#attributes' do
context 'when issue is open' do context 'when issue is open' do
let(:raw_data) { OpenStruct.new(base_data.merge(state: 'open')) } let(:raw_data) { double(base_data.merge(state: 'open')) }
it 'returns formatted attributes' do it 'returns formatted attributes' do
expected = { expected = {
...@@ -48,7 +49,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do ...@@ -48,7 +49,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do
context 'when issue is closed' do context 'when issue is closed' do
let(:closed_at) { DateTime.strptime('2011-01-28T19:01:12Z') } let(:closed_at) { DateTime.strptime('2011-01-28T19:01:12Z') }
let(:raw_data) { OpenStruct.new(base_data.merge(state: 'closed', closed_at: closed_at)) } let(:raw_data) { double(base_data.merge(state: 'closed', closed_at: closed_at)) }
it 'returns formatted attributes' do it 'returns formatted attributes' do
expected = { expected = {
...@@ -69,7 +70,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do ...@@ -69,7 +70,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do
end end
context 'when it is assigned to someone' do context 'when it is assigned to someone' do
let(:raw_data) { OpenStruct.new(base_data.merge(assignee: octocat)) } let(:raw_data) { double(base_data.merge(assignee: octocat)) }
it 'returns nil as assignee_id when is not a GitLab user' do it 'returns nil as assignee_id when is not a GitLab user' do
expect(issue.attributes.fetch(:assignee_id)).to be_nil expect(issue.attributes.fetch(:assignee_id)).to be_nil
...@@ -83,8 +84,8 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do ...@@ -83,8 +84,8 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do
end end
context 'when it has a milestone' do context 'when it has a milestone' do
let(:milestone) { OpenStruct.new(number: 45) } let(:milestone) { double(number: 45) }
let(:raw_data) { OpenStruct.new(base_data.merge(milestone: milestone)) } let(:raw_data) { double(base_data.merge(milestone: milestone)) }
it 'returns nil when milestone does not exist' do it 'returns nil when milestone does not exist' do
expect(issue.attributes.fetch(:milestone)).to be_nil expect(issue.attributes.fetch(:milestone)).to be_nil
...@@ -98,7 +99,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do ...@@ -98,7 +99,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do
end end
context 'when author is a GitLab user' do context 'when author is a GitLab user' do
let(:raw_data) { OpenStruct.new(base_data.merge(user: octocat)) } let(:raw_data) { double(base_data.merge(user: octocat)) }
it 'returns project#creator_id as author_id when is not a GitLab user' do it 'returns project#creator_id as author_id when is not a GitLab user' do
expect(issue.attributes.fetch(:author_id)).to eq project.creator_id expect(issue.attributes.fetch(:author_id)).to eq project.creator_id
...@@ -114,7 +115,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do ...@@ -114,7 +115,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do
describe '#has_comments?' do describe '#has_comments?' do
context 'when number of comments is greater than zero' do context 'when number of comments is greater than zero' do
let(:raw_data) { OpenStruct.new(base_data.merge(comments: 1)) } let(:raw_data) { double(base_data.merge(comments: 1)) }
it 'returns true' do it 'returns true' do
expect(issue.has_comments?).to eq true expect(issue.has_comments?).to eq true
...@@ -122,7 +123,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do ...@@ -122,7 +123,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do
end end
context 'when number of comments is equal to zero' do context 'when number of comments is equal to zero' do
let(:raw_data) { OpenStruct.new(base_data.merge(comments: 0)) } let(:raw_data) { double(base_data.merge(comments: 0)) }
it 'returns false' do it 'returns false' do
expect(issue.has_comments?).to eq false expect(issue.has_comments?).to eq false
...@@ -131,7 +132,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do ...@@ -131,7 +132,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do
end end
describe '#number' do describe '#number' do
let(:raw_data) { OpenStruct.new(base_data.merge(number: 1347)) } let(:raw_data) { double(base_data.merge(number: 1347)) }
it 'returns pull request number' do it 'returns pull request number' do
expect(issue.number).to eq 1347 expect(issue.number).to eq 1347
...@@ -140,7 +141,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do ...@@ -140,7 +141,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do
describe '#valid?' do describe '#valid?' do
context 'when mention a pull request' do context 'when mention a pull request' do
let(:raw_data) { OpenStruct.new(base_data.merge(pull_request: OpenStruct.new)) } let(:raw_data) { double(base_data.merge(pull_request: double)) }
it 'returns false' do it 'returns false' do
expect(issue.valid?).to eq false expect(issue.valid?).to eq false
...@@ -148,7 +149,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do ...@@ -148,7 +149,7 @@ describe Gitlab::GithubImport::IssueFormatter, lib: true do
end end
context 'when does not mention a pull request' do context 'when does not mention a pull request' do
let(:raw_data) { OpenStruct.new(base_data.merge(pull_request: nil)) } let(:raw_data) { double(base_data.merge(pull_request: nil)) }
it 'returns true' do it 'returns true' do
expect(issue.valid?).to eq true expect(issue.valid?).to eq true
......
...@@ -2,17 +2,18 @@ require 'spec_helper' ...@@ -2,17 +2,18 @@ require 'spec_helper'
describe Gitlab::GithubImport::PullRequestFormatter, lib: true do describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:repository) { OpenStruct.new(id: 1, fork: false) } let(:repository) { double(id: 1, fork: false) }
let(:source_repo) { repository } let(:source_repo) { repository }
let(:source_branch) { OpenStruct.new(ref: 'feature', repo: source_repo) } let(:source_branch) { double(ref: 'feature', repo: source_repo) }
let(:target_repo) { repository } let(:target_repo) { repository }
let(:target_branch) { OpenStruct.new(ref: 'master', repo: target_repo) } let(:target_branch) { double(ref: 'master', repo: target_repo) }
let(:octocat) { OpenStruct.new(id: 123456, login: 'octocat') } let(:octocat) { double(id: 123456, login: 'octocat') }
let(:created_at) { DateTime.strptime('2011-01-26T19:01:12Z') } let(:created_at) { DateTime.strptime('2011-01-26T19:01:12Z') }
let(:updated_at) { DateTime.strptime('2011-01-27T19:01:12Z') } let(:updated_at) { DateTime.strptime('2011-01-27T19:01:12Z') }
let(:base_data) do let(:base_data) do
{ {
number: 1347, number: 1347,
milestone: nil,
state: 'open', state: 'open',
title: 'New feature', title: 'New feature',
body: 'Please pull these awesome changes', body: 'Please pull these awesome changes',
...@@ -31,7 +32,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -31,7 +32,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
describe '#attributes' do describe '#attributes' do
context 'when pull request is open' do context 'when pull request is open' do
let(:raw_data) { OpenStruct.new(base_data.merge(state: 'open')) } let(:raw_data) { double(base_data.merge(state: 'open')) }
it 'returns formatted attributes' do it 'returns formatted attributes' do
expected = { expected = {
...@@ -56,7 +57,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -56,7 +57,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
context 'when pull request is closed' do context 'when pull request is closed' do
let(:closed_at) { DateTime.strptime('2011-01-28T19:01:12Z') } let(:closed_at) { DateTime.strptime('2011-01-28T19:01:12Z') }
let(:raw_data) { OpenStruct.new(base_data.merge(state: 'closed', closed_at: closed_at)) } let(:raw_data) { double(base_data.merge(state: 'closed', closed_at: closed_at)) }
it 'returns formatted attributes' do it 'returns formatted attributes' do
expected = { expected = {
...@@ -81,7 +82,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -81,7 +82,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
context 'when pull request is merged' do context 'when pull request is merged' do
let(:merged_at) { DateTime.strptime('2011-01-28T13:01:12Z') } let(:merged_at) { DateTime.strptime('2011-01-28T13:01:12Z') }
let(:raw_data) { OpenStruct.new(base_data.merge(state: 'closed', merged_at: merged_at)) } let(:raw_data) { double(base_data.merge(state: 'closed', merged_at: merged_at)) }
it 'returns formatted attributes' do it 'returns formatted attributes' do
expected = { expected = {
...@@ -105,7 +106,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -105,7 +106,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
context 'when it is assigned to someone' do context 'when it is assigned to someone' do
let(:raw_data) { OpenStruct.new(base_data.merge(assignee: octocat)) } let(:raw_data) { double(base_data.merge(assignee: octocat)) }
it 'returns nil as assignee_id when is not a GitLab user' do it 'returns nil as assignee_id when is not a GitLab user' do
expect(pull_request.attributes.fetch(:assignee_id)).to be_nil expect(pull_request.attributes.fetch(:assignee_id)).to be_nil
...@@ -119,7 +120,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -119,7 +120,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
context 'when author is a GitLab user' do context 'when author is a GitLab user' do
let(:raw_data) { OpenStruct.new(base_data.merge(user: octocat)) } let(:raw_data) { double(base_data.merge(user: octocat)) }
it 'returns project#creator_id as author_id when is not a GitLab user' do it 'returns project#creator_id as author_id when is not a GitLab user' do
expect(pull_request.attributes.fetch(:author_id)).to eq project.creator_id expect(pull_request.attributes.fetch(:author_id)).to eq project.creator_id
...@@ -133,8 +134,8 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -133,8 +134,8 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
context 'when it has a milestone' do context 'when it has a milestone' do
let(:milestone) { OpenStruct.new(number: 45) } let(:milestone) { double(number: 45) }
let(:raw_data) { OpenStruct.new(base_data.merge(milestone: milestone)) } let(:raw_data) { double(base_data.merge(milestone: milestone)) }
it 'returns nil when milestone does not exists' do it 'returns nil when milestone does not exists' do
expect(pull_request.attributes.fetch(:milestone)).to be_nil expect(pull_request.attributes.fetch(:milestone)).to be_nil
...@@ -149,7 +150,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -149,7 +150,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
describe '#number' do describe '#number' do
let(:raw_data) { OpenStruct.new(base_data.merge(number: 1347)) } let(:raw_data) { double(base_data.merge(number: 1347)) }
it 'returns pull request number' do it 'returns pull request number' do
expect(pull_request.number).to eq 1347 expect(pull_request.number).to eq 1347
...@@ -157,11 +158,11 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -157,11 +158,11 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
describe '#valid?' do describe '#valid?' do
let(:invalid_branch) { OpenStruct.new(ref: 'invalid-branch') } let(:invalid_branch) { double(ref: 'invalid-branch').as_null_object }
context 'when source, and target repositories are the same' do context 'when source, and target repositories are the same' do
context 'and source and target branches exists' do context 'and source and target branches exists' do
let(:raw_data) { OpenStruct.new(base_data.merge(head: source_branch, base: target_branch)) } let(:raw_data) { double(base_data.merge(head: source_branch, base: target_branch)) }
it 'returns true' do it 'returns true' do
expect(pull_request.valid?).to eq true expect(pull_request.valid?).to eq true
...@@ -169,7 +170,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -169,7 +170,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
context 'and source branch doesn not exists' do context 'and source branch doesn not exists' do
let(:raw_data) { OpenStruct.new(base_data.merge(head: invalid_branch, base: target_branch)) } let(:raw_data) { double(base_data.merge(head: invalid_branch, base: target_branch)) }
it 'returns false' do it 'returns false' do
expect(pull_request.valid?).to eq false expect(pull_request.valid?).to eq false
...@@ -177,7 +178,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -177,7 +178,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
context 'and target branch doesn not exists' do context 'and target branch doesn not exists' do
let(:raw_data) { OpenStruct.new(base_data.merge(head: source_branch, base: invalid_branch)) } let(:raw_data) { double(base_data.merge(head: source_branch, base: invalid_branch)) }
it 'returns false' do it 'returns false' do
expect(pull_request.valid?).to eq false expect(pull_request.valid?).to eq false
...@@ -186,8 +187,8 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -186,8 +187,8 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
context 'when source repo is a fork' do context 'when source repo is a fork' do
let(:source_repo) { OpenStruct.new(id: 2, fork: true) } let(:source_repo) { double(id: 2, fork: true) }
let(:raw_data) { OpenStruct.new(base_data) } let(:raw_data) { double(base_data) }
it 'returns false' do it 'returns false' do
expect(pull_request.valid?).to eq false expect(pull_request.valid?).to eq false
...@@ -195,8 +196,8 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -195,8 +196,8 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
context 'when target repo is a fork' do context 'when target repo is a fork' do
let(:target_repo) { OpenStruct.new(id: 2, fork: true) } let(:target_repo) { double(id: 2, fork: true) }
let(:raw_data) { OpenStruct.new(base_data) } let(:raw_data) { double(base_data) }
it 'returns false' do it 'returns false' do
expect(pull_request.valid?).to eq false expect(pull_request.valid?).to eq false
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment