Skip to content

Commit

Permalink
(SDK-313) refactor cli/tests/unit_spec.rb
Browse files Browse the repository at this point in the history
* extract common pre-conditions
* change SystemExit to detect missing exceptions
* move explicit $stdout expectations to output matchers
  • Loading branch information
DavidS committed Aug 8, 2017
1 parent 9c07470 commit 2dfc9a1
Showing 1 changed file with 53 additions and 62 deletions.
115 changes: 53 additions & 62 deletions spec/unit/cli/test/unit_spec.rb
Original file line number Diff line number Diff line change
@@ -1,96 +1,87 @@
require 'spec_helper'
require 'pdk/tests/unit'

describe 'Running `pdk test unit`' do
describe '`pdk test unit`' do
subject(:test_unit_cmd) { PDK::CLI.instance_variable_get(:@test_unit_cmd) }

it { is_expected.not_to be_nil }

context 'with --help' do
it do
begin
expect {
PDK::CLI.run(['test', 'unit', '--help'])
}.to output(%r{^USAGE\s+pdk test unit}m).to_stdout
rescue SystemExit => e
expect {
PDK::CLI.run(['test', 'unit', '--help'])
}.to raise_error(SystemExit) { |e|
expect(e.status).to eq 0
end
}.and output(%r{^USAGE\s+pdk test unit}m).to_stdout
end
end

context 'when listing tests' do
let(:args) { ['--list'] }

context 'when executing' do
before(:each) do
expect(PDK::CLI::Util).to receive(:ensure_in_module!).with(no_args).once
end

context 'when no tests are found' do
before(:each) do
expect(PDK::Test::Unit).to receive(:list).with(no_args).once.and_return([])
expect($stdout).to receive(:puts).with(%r{No examples found})
end

it { test_unit_cmd.run_this(args) }
end
context 'when listing tests' do
let(:args) { ['--list'] }

context 'when some tests are found' do
let(:test_list) { [{ id: 'first_id', full_description: 'first_description' }, { id: 'second_id', full_description: 'second_description' }] }
context 'when no tests are found' do
before(:each) do
expect(PDK::Test::Unit).to receive(:list).with(no_args).once.and_return([])
end

before(:each) do
expect(PDK::Test::Unit).to receive(:list).with(no_args).once.and_return(test_list)
expect($stdout).to receive(:puts).with('Examples:')
expect($stdout).to receive(:puts).with(%r{first_id\tfirst_description})
expect($stdout).to receive(:puts).with(%r{second_id\tsecond_description})
it { expect { test_unit_cmd.run_this(args) }.to output(%r{No examples found}m).to_stdout }
end

it { test_unit_cmd.run_this(args) }
end
end

context 'when running tests' do
context 'when tests pass' do
before(:each) do
expect(PDK::CLI::Util).to receive(:ensure_in_module!).with(no_args).once
expect(PDK::Test::Unit).to receive(:invoke).with(instance_of(PDK::Report), hash_including(:tests)).once.and_return(0)
end
context 'when some tests are found' do
let(:test_list) { [{ id: 'first_id', full_description: 'first_description' }, { id: 'second_id', full_description: 'second_description' }] }

it 'returns 0' do
begin
test_unit_cmd.run_this([])
rescue SystemExit => e
expect(e.status).to eq 0
before(:each) do
expect(PDK::Test::Unit).to receive(:list).with(no_args).once.and_return(test_list)
end

it { expect { test_unit_cmd.run_this(args) }.to output(%r{Examples:\nfirst_id\tfirst_description\nsecond_id\tsecond_description}m).to_stdout }
end
end

context 'when tests pass, with a format option' do
before(:each) do
expect(PDK::CLI::Util).to receive(:ensure_in_module!).with(no_args).once
expect(PDK::CLI::Util::OptionNormalizer).to receive(:report_formats).with(['text:results.txt']).and_return([{ method: :write_text, target: 'results.txt' }]).twice
expect(PDK::Test::Unit).to receive(:invoke).with(instance_of(PDK::Report), hash_including(:tests)).once.and_return(0)
end
context 'when running tests' do
context 'when tests pass' do
before(:each) do
expect(PDK::Test::Unit).to receive(:invoke).with(instance_of(PDK::Report), hash_including(:tests)).once.and_return(0)
end

it 'returns 0' do
begin
test_unit_cmd.run_this(['--format=text:results.txt'])
rescue SystemExit => e
expect(e.status).to eq 0
it do
expect {
test_unit_cmd.run_this([])
}.to raise_error(SystemExit) { |e|
expect(e.status).to eq 0
}
end
end
end

context 'when tests fail' do
before(:each) do
expect(PDK::CLI::Util).to receive(:ensure_in_module!).with(no_args).once
expect(PDK::Test::Unit).to receive(:invoke).with(instance_of(PDK::Report), hash_including(:tests)).once.and_return(1)
context 'with a format option' do
before(:each) do
expect(PDK::CLI::Util::OptionNormalizer).to receive(:report_formats).with(['text:results.txt']).and_return([{ method: :write_text, target: 'results.txt' }]).twice
end
it do
expect {
test_unit_cmd.run_this(['--format=text:results.txt'])
}.to raise_error(SystemExit) { |e|
expect(e.status).to eq 0
}
end
end
end

it 'does not return 0' do
begin
test_unit_cmd.run_this([])
rescue SystemExit => e
expect(e.status).not_to eq 0
context 'when tests fail' do
before(:each) do
expect(PDK::Test::Unit).to receive(:invoke).with(instance_of(PDK::Report), hash_including(:tests)).once.and_return(1)
end

it do
expect {
test_unit_cmd.run_this([])
}.to raise_error(SystemExit) { |e|
expect(e.status).not_to eq 0
}
end
end
end
Expand Down

0 comments on commit 2dfc9a1

Please sign in to comment.