Merge branch 'spec-refactor-shared_examples'
Refactor specs with shared examples.
This commit is contained in:
commit
9a71beefff
@ -2,46 +2,6 @@ require 'spec_helper'
|
||||
|
||||
module Producer::Core
|
||||
describe Action do
|
||||
let(:input) { StringIO.new }
|
||||
let(:output) { StringIO.new }
|
||||
let(:env) { Env.new(input: input, output: output) }
|
||||
let(:arguments) { [:some, :arguments] }
|
||||
subject(:action) { Action.new(env, *arguments) }
|
||||
|
||||
describe '#env' do
|
||||
it 'returns the assigned env' do
|
||||
expect(action.env).to be env
|
||||
end
|
||||
end
|
||||
|
||||
describe '#arguments' do
|
||||
it 'returns the assigned arguments' do
|
||||
expect(action.arguments).to eq arguments
|
||||
end
|
||||
end
|
||||
|
||||
describe '#input' do
|
||||
it 'returns env input' do
|
||||
expect(action.input).to be input
|
||||
end
|
||||
end
|
||||
|
||||
describe '#output' do
|
||||
it 'returns env output' do
|
||||
expect(action.output).to be output
|
||||
end
|
||||
end
|
||||
|
||||
describe '#remote' do
|
||||
it 'returns env remote' do
|
||||
expect(action.remote).to be action.env.remote
|
||||
end
|
||||
end
|
||||
|
||||
describe '#fs' do
|
||||
it 'returns env remote fs' do
|
||||
expect(action.fs).to be action.env.remote.fs
|
||||
end
|
||||
end
|
||||
it_behaves_like 'action'
|
||||
end
|
||||
end
|
||||
|
@ -7,6 +7,8 @@ module Producer::Core
|
||||
let(:text) { 'hello' }
|
||||
subject(:echo) { Echo.new(env, text) }
|
||||
|
||||
it_behaves_like 'action'
|
||||
|
||||
describe '#apply' do
|
||||
it 'writes the given string to env output with a record separator' do
|
||||
echo.apply
|
||||
|
@ -8,6 +8,8 @@ module Producer::Core
|
||||
let(:content) { 'some_content' }
|
||||
subject(:writer) { FileWriter.new(env, path, content) }
|
||||
|
||||
it_behaves_like 'action'
|
||||
|
||||
describe '#apply' do
|
||||
it 'writes content to file on remote filesystem' do
|
||||
expect(writer.fs).to receive(:file_write).with(path, content)
|
||||
|
@ -7,6 +7,8 @@ module Producer::Core
|
||||
let(:path) { 'some_path' }
|
||||
subject(:mkdir) { Mkdir.new(env, path) }
|
||||
|
||||
it_behaves_like 'action'
|
||||
|
||||
describe '#apply' do
|
||||
it 'creates directory on remote filesystem' do
|
||||
expect(mkdir.fs).to receive(:mkdir).with(path)
|
||||
|
@ -8,6 +8,8 @@ module Producer::Core
|
||||
let(:command) { "echo #{command_args}" }
|
||||
subject(:sh) { ShellCommand.new(env, command) }
|
||||
|
||||
it_behaves_like 'action'
|
||||
|
||||
describe '#apply' do
|
||||
it 'executes the remote command' do
|
||||
expect(sh.remote).to receive(:execute).with(command)
|
||||
|
@ -2,82 +2,6 @@ require 'spec_helper'
|
||||
|
||||
module Producer::Core
|
||||
describe Test do
|
||||
let(:env) { Env.new }
|
||||
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
|
||||
it_behaves_like 'test'
|
||||
end
|
||||
end
|
||||
|
@ -8,9 +8,7 @@ module Producer::Core
|
||||
let(:content) { 'some_content' }
|
||||
subject(:test) { FileContains.new(env, filepath, content) }
|
||||
|
||||
it 'is a kind of test' do
|
||||
expect(test).to be_a Test
|
||||
end
|
||||
it_behaves_like 'test'
|
||||
|
||||
describe '#verify' do
|
||||
let(:fs) { double 'fs' }
|
||||
|
@ -7,9 +7,7 @@ module Producer::Core
|
||||
let(:path) { 'some_directory' }
|
||||
subject(:has_dir) { HasDir.new(env, path) }
|
||||
|
||||
it 'is a kind of test' do
|
||||
expect(has_dir).to be_a Test
|
||||
end
|
||||
it_behaves_like 'test'
|
||||
|
||||
describe '#verify', :ssh do
|
||||
before { sftp_story }
|
||||
|
@ -7,9 +7,7 @@ module Producer::Core
|
||||
let(:variable_name) { :some_variable_name }
|
||||
subject(:has_env) { HasEnv.new(env, variable_name) }
|
||||
|
||||
it 'is a kind of test' do
|
||||
expect(has_env).to be_a Test
|
||||
end
|
||||
it_behaves_like 'test'
|
||||
|
||||
describe '#verify' do
|
||||
let(:environment) { double 'environment' }
|
||||
|
@ -7,9 +7,7 @@ module Producer::Core
|
||||
let(:filepath) { 'some_file' }
|
||||
subject(:has_file) { HasFile.new(env, filepath) }
|
||||
|
||||
it 'is a kind of test' do
|
||||
expect(has_file).to be_a Test
|
||||
end
|
||||
it_behaves_like 'test'
|
||||
|
||||
describe '#verify', :ssh do
|
||||
before { sftp_story }
|
||||
|
45
spec/support/shared_action.rb
Normal file
45
spec/support/shared_action.rb
Normal file
@ -0,0 +1,45 @@
|
||||
module Producer::Core
|
||||
shared_examples 'action' do
|
||||
let(:input) { StringIO.new }
|
||||
let(:output) { StringIO.new }
|
||||
let(:env) { Env.new(input: input, output: output) }
|
||||
let(:arguments) { [:some, :arguments] }
|
||||
subject(:action) { described_class.new(env, *arguments) }
|
||||
|
||||
describe '#env' do
|
||||
it 'returns the assigned env' do
|
||||
expect(action.env).to be env
|
||||
end
|
||||
end
|
||||
|
||||
describe '#arguments' do
|
||||
it 'returns the assigned arguments' do
|
||||
expect(action.arguments).to eq arguments
|
||||
end
|
||||
end
|
||||
|
||||
describe '#input' do
|
||||
it 'returns env input' do
|
||||
expect(action.input).to be input
|
||||
end
|
||||
end
|
||||
|
||||
describe '#output' do
|
||||
it 'returns env output' do
|
||||
expect(action.output).to be output
|
||||
end
|
||||
end
|
||||
|
||||
describe '#remote' do
|
||||
it 'returns env remote' do
|
||||
expect(action.remote).to be action.env.remote
|
||||
end
|
||||
end
|
||||
|
||||
describe '#fs' do
|
||||
it 'returns env remote fs' do
|
||||
expect(action.fs).to be action.env.remote.fs
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
81
spec/support/shared_test.rb
Normal file
81
spec/support/shared_test.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user