From 2b99fa533da7e4d59edc015cf8c637c94fa2d496 Mon Sep 17 00:00:00 2001 From: Ryota Ikezawa Date: Sun, 5 Aug 2018 03:03:55 +0900 Subject: [PATCH] Improve tests --- spec/lgtm_spec.rb | 97 +++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/spec/lgtm_spec.rb b/spec/lgtm_spec.rb index f359c4c..239a0d9 100644 --- a/spec/lgtm_spec.rb +++ b/spec/lgtm_spec.rb @@ -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("

LGTM

") + 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("

LGTM

") 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