Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paveg committed Aug 4, 2018
1 parent 51c6d05 commit 2b99fa5
Showing 1 changed file with 47 additions and 50 deletions.
97 changes: 47 additions & 50 deletions spec/lgtm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,67 @@

require File.expand_path('spec_helper', __dir__)

module Danger
describe Danger::DangerLgtm do
it 'should be a plugin' do
expect(Danger::DangerLgtm.new(nil)).to be_a Danger::Plugin
end
describe Danger::DangerLgtm do
def mock(request_url: 'https://lgtm.in/p/sSuI4hm0q',
actual_image_url: 'https://example.com/image.jpg')
double(
:[] => request_url,
body: JSON.generate(
actualImageUrl: actual_image_url
)
)
end

#
# You should test your custom attributes and methods here
#
describe 'with Dangerfile' do
before do
@dangerfile = testing_dangerfile
@lgtm = @dangerfile.lgtm
end
it 'should be a plugin' do
expect(Danger::DangerLgtm.new(nil)).to be_a(Danger::Plugin)
end

# Some examples for writing tests
# You should replace these with your own.
describe '#check_lgtm' do
subject { lgtm.check_lgtm(https_image_only: https_image_only, image_url: image_url) }

it 'lgtm with no violation' do
@lgtm.check_lgtm
expect(@dangerfile.status_report[:markdowns].length).to eq(1)
end
let(:dangerfile) { testing_dangerfile }
let(:lgtm) { dangerfile.lgtm }
let(:https_image_only) { false } # default false
let(:image_url) {} # default nil

it 'lgtm with errors' do
allow(@lgtm).to receive(:validate_response).and_raise(::Lgtm::Errors::UnexpectedError)

@lgtm.check_lgtm
shared_examples 'returns correctly message' do
it do
allow(Net::HTTP).to receive(:start).and_return(mock)
is_expected

expect(@dangerfile.status_report[:markdowns][0].message)
.to eq("<h1 align='center'>LGTM</h1>")
expect(dangerfile.status_report[:markdowns][0].message)
.to match(expected_message)
end
end

def mock(request_url: 'https://lgtm.in/p/sSuI4hm0q',
actual_image_url: 'https://example.com/image.jpg')
double(
:[] => request_url,
body: JSON.generate(
actualImageUrl: actual_image_url
)
)
context 'with Dangerfile' do
it 'when no violation' do
is_expected
expect(dangerfile.status_report[:markdowns].length).to eq(1)
end

it 'pick random pic from lgtm.in' do
allow(Net::HTTP).to receive(:start).and_return(mock)

@lgtm.check_lgtm

expect(@dangerfile.status_report[:markdowns][0].message)
.to match(%r{https:\/\/example.com\/image.jpg})
it 'lgtm with errors' do
allow(lgtm).to receive(:validate_response).and_raise(::Lgtm::Errors::UnexpectedError)
is_expected
expect(dangerfile.status_report[:markdowns][0].message)
.to eq("<h1 align='center'>LGTM</h1>")
end

it 'pick random pic from lgtm.in with https_image_only option' do
allow(Net::HTTP).to receive(:start).and_return(mock)

@lgtm.check_lgtm https_image_only: true
context 'pick random pic from lgtm.in' do
let(:expected_message) { %r{https:\/\/example.com\/image.jpg} }
it_behaves_like 'returns correctly message'
end

expect(@dangerfile.status_report[:markdowns][0].message)
.to match(%r{https:\/\/example.com\/image.jpg})
context 'pick random pic from lgtm.in with https_image_only option' do
let(:https_image_only) { true }
let(:expected_message) { %r{https:\/\/example.com\/image.jpg} }
it_behaves_like 'returns correctly message'
end

it 'use given url' do
@lgtm.check_lgtm image_url: 'http://imgur.com/Irk2wyX.jpg'
expect(@dangerfile.status_report[:markdowns][0].message)
.to match(%r{http:\/\/imgur\.com\/Irk2wyX\.jpg})
context 'use given url' do
let(:image_url) { 'http://imgur.com/Irk2wyX.jpg' }
let(:expected_message) { %r{http:\/\/imgur\.com\/Irk2wyX\.jpg} }
it_behaves_like 'returns correctly message'
end
end
end
Expand Down

0 comments on commit 2b99fa5

Please sign in to comment.