Refactor tests specs with shared examples

This commit is contained in:
Thibault Jouan 2014-02-10 17:33:48 +00:00
parent 4624a81100
commit 91637ca631
6 changed files with 86 additions and 89 deletions

View File

@ -2,82 +2,6 @@ require 'spec_helper'
module Producer::Core module Producer::Core
describe Test do describe Test do
let(:env) { Env.new } it_behaves_like 'test'
let(:arguments) { [:some, :arguments] }
subject(:test) { Test.new(env, *arguments) }
describe '#initialize' do
it 'assigns the env' do
expect(test.env).to be env
end
it 'assigns the arguments' do
expect(test.arguments).to eq arguments
end
it 'assigns negated as false by default' do
expect(test).to_not be_negated
end
context 'when negated option is true' do
subject(:test) { described_class.new(env, *arguments, negated: true) }
it 'assigns negated as true' do
expect(test).to be_negated
end
end
end
describe '#remote' do
it 'returns env remote' do
expect(test.remote).to be test.env.remote
end
end
describe '#fs' do
it 'returns env remote fs' do
expect(test.fs).to be test.env.remote.fs
end
end
describe '#negated?' do
it 'returns false' do
expect(test.negated?).to be false
end
context 'when test is negated' do
subject(:test) { described_class.new(env, *arguments, negated: true) }
it 'returns true' do
expect(test.negated?).to be true
end
end
end
describe '#pass?' do
it 'returns true when #verify is true' do
allow(test).to receive(:verify) { true }
expect(test.pass?).to be true
end
it 'returns false when #verify is false' do
allow(test).to receive(:verify) { false }
expect(test.pass?).to be false
end
context 'when test is negated' do
subject(:test) { described_class.new(env, *arguments, negated: true) }
it 'returns false when #verify is true' do
allow(test).to receive(:verify) { true }
expect(test.pass?).to be false
end
it 'returns true when #verify is false' do
allow(test).to receive(:verify) { false }
expect(test.pass?).to be true
end
end
end
end end
end end

View File

@ -8,9 +8,7 @@ module Producer::Core
let(:content) { 'some_content' } let(:content) { 'some_content' }
subject(:test) { FileContains.new(env, filepath, content) } subject(:test) { FileContains.new(env, filepath, content) }
it 'is a kind of test' do it_behaves_like 'test'
expect(test).to be_a Test
end
describe '#verify' do describe '#verify' do
let(:fs) { double 'fs' } let(:fs) { double 'fs' }

View File

@ -7,9 +7,7 @@ module Producer::Core
let(:path) { 'some_directory' } let(:path) { 'some_directory' }
subject(:has_dir) { HasDir.new(env, path) } subject(:has_dir) { HasDir.new(env, path) }
it 'is a kind of test' do it_behaves_like 'test'
expect(has_dir).to be_a Test
end
describe '#verify', :ssh do describe '#verify', :ssh do
before { sftp_story } before { sftp_story }

View File

@ -7,9 +7,7 @@ module Producer::Core
let(:variable_name) { :some_variable_name } let(:variable_name) { :some_variable_name }
subject(:has_env) { HasEnv.new(env, variable_name) } subject(:has_env) { HasEnv.new(env, variable_name) }
it 'is a kind of test' do it_behaves_like 'test'
expect(has_env).to be_a Test
end
describe '#verify' do describe '#verify' do
let(:environment) { double 'environment' } let(:environment) { double 'environment' }

View File

@ -7,9 +7,7 @@ module Producer::Core
let(:filepath) { 'some_file' } let(:filepath) { 'some_file' }
subject(:has_file) { HasFile.new(env, filepath) } subject(:has_file) { HasFile.new(env, filepath) }
it 'is a kind of test' do it_behaves_like 'test'
expect(has_file).to be_a Test
end
describe '#verify', :ssh do describe '#verify', :ssh do
before { sftp_story } before { sftp_story }

View File

@ -0,0 +1,81 @@
module Producer::Core
shared_examples 'test' do
let(:env) { Env.new }
let(:arguments) { [:some, :arguments] }
subject(:test) { described_class.new(env, *arguments) }
describe '#initialize' do
it 'assigns the env' do
expect(test.env).to be env
end
it 'assigns the arguments' do
expect(test.arguments).to eq arguments
end
it 'assigns negated as false by default' do
expect(test).to_not be_negated
end
context 'when negated option is true' do
subject(:test) { described_class.new(env, *arguments, negated: true) }
it 'assigns negated as true' do
expect(test).to be_negated
end
end
end
describe '#remote' do
it 'returns env remote' do
expect(test.remote).to be test.env.remote
end
end
describe '#fs' do
it 'returns env remote fs' do
expect(test.fs).to be test.env.remote.fs
end
end
describe '#negated?' do
it 'returns false' do
expect(test.negated?).to be false
end
context 'when test is negated' do
subject(:test) { described_class.new(env, *arguments, negated: true) }
it 'returns true' do
expect(test.negated?).to be true
end
end
end
describe '#pass?' do
it 'returns true when #verify is true' do
allow(test).to receive(:verify) { true }
expect(test.pass?).to be true
end
it 'returns false when #verify is false' do
allow(test).to receive(:verify) { false }
expect(test.pass?).to be false
end
context 'when test is negated' do
subject(:test) { described_class.new(env, *arguments, negated: true) }
it 'returns false when #verify is true' do
allow(test).to receive(:verify) { true }
expect(test.pass?).to be false
end
it 'returns true when #verify is false' do
allow(test).to receive(:verify) { false }
expect(test.pass?).to be true
end
end
end
end
end