|
2 | 2 |
|
3 | 3 | require File.expand_path('spec_helper', __dir__) |
4 | 4 |
|
5 | | -module Danger |
6 | | - describe Danger::DangerLgtm do |
7 | | - it 'should be a plugin' do |
8 | | - expect(Danger::DangerLgtm.new(nil)).to be_a Danger::Plugin |
9 | | - end |
10 | | - |
11 | | - # |
12 | | - # You should test your custom attributes and methods here |
13 | | - # |
14 | | - describe 'with Dangerfile' do |
15 | | - before do |
16 | | - @dangerfile = testing_dangerfile |
17 | | - @lgtm = @dangerfile.lgtm |
18 | | - end |
| 5 | +describe Danger::DangerLgtm do |
| 6 | + def mock(request_url: 'https://lgtm.in/p/sSuI4hm0q', |
| 7 | + actual_image_url: 'https://example.com/image.jpg') |
| 8 | + double( |
| 9 | + :[] => request_url, |
| 10 | + body: JSON.generate( |
| 11 | + actualImageUrl: actual_image_url |
| 12 | + ) |
| 13 | + ) |
| 14 | + end |
19 | 15 |
|
20 | | - # Some examples for writing tests |
21 | | - # You should replace these with your own. |
| 16 | + it 'should be a plugin' do |
| 17 | + expect(Danger::DangerLgtm.new(nil)).to be_a(Danger::Plugin) |
| 18 | + end |
22 | 19 |
|
23 | | - it 'lgtm with no violation' do |
24 | | - @lgtm.check_lgtm |
25 | | - expect(@dangerfile.status_report[:markdowns].length).to eq(1) |
26 | | - end |
| 20 | + describe '#check_lgtm' do |
| 21 | + subject do |
| 22 | + lgtm.check_lgtm( |
| 23 | + https_image_only: https_image_only, |
| 24 | + image_url: image_url |
| 25 | + ) |
| 26 | + end |
27 | 27 |
|
28 | | - it 'lgtm with default url is OverQuota' do |
29 | | - allow(Net::HTTP).to receive(:start).and_return(mock(code: '503')) |
| 28 | + let(:dangerfile) { testing_dangerfile } |
| 29 | + let(:lgtm) { dangerfile.lgtm } |
| 30 | + let(:https_image_only) { false } # default false |
| 31 | + let(:image_url) {} # default nil |
30 | 32 |
|
31 | | - @lgtm.check_lgtm |
| 33 | + shared_examples 'returns correctly message' do |
| 34 | + it do |
| 35 | + allow(Net::HTTP).to receive(:start).and_return(mock) |
| 36 | + is_expected |
32 | 37 |
|
33 | | - expect(@dangerfile.status_report[:markdowns][0].message) |
34 | | - .to eq("<h1 align='center'>LGTM</h1>") |
| 38 | + expect(dangerfile.status_report[:markdowns][0].message) |
| 39 | + .to match(expected_message) |
35 | 40 | end |
| 41 | + end |
36 | 42 |
|
37 | | - def mock(request_url: 'https://lgtm.in/p/sSuI4hm0q', |
38 | | - actual_image_url: 'https://example.com/image.jpg', |
39 | | - code: '302') |
40 | | - double( |
41 | | - :[] => request_url, |
42 | | - body: JSON.generate( |
43 | | - actualImageUrl: actual_image_url |
44 | | - ), |
45 | | - code: code |
46 | | - ) |
| 43 | + context 'with Dangerfile' do |
| 44 | + it 'when no violation' do |
| 45 | + is_expected |
| 46 | + expect(dangerfile.status_report[:markdowns].length).to eq(1) |
47 | 47 | end |
48 | 48 |
|
49 | | - it 'pick random pic from lgtm.in' do |
50 | | - allow(Net::HTTP).to receive(:start).and_return(mock) |
51 | | - |
52 | | - @lgtm.check_lgtm |
53 | | - |
54 | | - expect(@dangerfile.status_report[:markdowns][0].message) |
55 | | - .to match(%r{https:\/\/example.com\/image.jpg}) |
| 49 | + it 'lgtm with errors' do |
| 50 | + allow(lgtm).to receive(:validate_response) |
| 51 | + .and_raise(::Lgtm::Errors::UnexpectedError) |
| 52 | + is_expected |
| 53 | + expect(dangerfile.status_report[:markdowns][0].message) |
| 54 | + .to eq("<h1 align='center'>LGTM</h1>") |
56 | 55 | end |
57 | 56 |
|
58 | | - it 'pick random pic from lgtm.in with https_image_only option' do |
59 | | - allow(Net::HTTP).to receive(:start).and_return(mock) |
60 | | - |
61 | | - @lgtm.check_lgtm https_image_only: true |
| 57 | + context 'pick random pic from lgtm.in' do |
| 58 | + let(:expected_message) { %r{https:\/\/example.com\/image.jpg} } |
| 59 | + it_behaves_like 'returns correctly message' |
| 60 | + end |
62 | 61 |
|
63 | | - expect(@dangerfile.status_report[:markdowns][0].message) |
64 | | - .to match(%r{https:\/\/example.com\/image.jpg}) |
| 62 | + context 'pick random pic from lgtm.in with https_image_only option' do |
| 63 | + let(:https_image_only) { true } |
| 64 | + let(:expected_message) { %r{https:\/\/example.com\/image.jpg} } |
| 65 | + it_behaves_like 'returns correctly message' |
65 | 66 | end |
66 | 67 |
|
67 | | - it 'use given url' do |
68 | | - @lgtm.check_lgtm image_url: 'http://imgur.com/Irk2wyX.jpg' |
69 | | - expect(@dangerfile.status_report[:markdowns][0].message) |
70 | | - .to match(%r{http:\/\/imgur\.com\/Irk2wyX\.jpg}) |
| 68 | + context 'use given url' do |
| 69 | + let(:image_url) { 'http://imgur.com/Irk2wyX.jpg' } |
| 70 | + let(:expected_message) { %r{http:\/\/imgur\.com\/Irk2wyX\.jpg} } |
| 71 | + it_behaves_like 'returns correctly message' |
71 | 72 | end |
72 | 73 | end |
73 | 74 | end |
|
0 commit comments