Implement rspec TestEnvHelpers:

Allow use of our test environment, with a mocked remote and convenient
methods to write more concise expectations.

  #env, builds and returns a memoized test environment (with mocked
  remote)

  #output, returns env output as a string

  #remote_fs, returns env remote fs

  #expect_execution, setup a remote command execution expectation
This commit is contained in:
Thibault Jouan
2014-02-14 18:24:42 +00:00
parent 85e3bf2eac
commit fcba1a5c0b
11 changed files with 64 additions and 45 deletions

View File

@@ -2,8 +2,7 @@ require 'spec_helper'
module Producer::Core
module Tests
describe FileContains do
let(:env) { Env.new }
describe FileContains, :env do
let(:filepath) { 'some_file' }
let(:content) { 'some_content' }
subject(:test) { FileContains.new(env, filepath, content) }
@@ -11,13 +10,9 @@ module Producer::Core
it_behaves_like 'test'
describe '#verify' do
let(:fs) { double 'fs' }
before { allow(test).to receive(:fs) { fs } }
context 'when file contains the content' do
before do
allow(fs)
allow(remote_fs)
.to receive(:file_read).with(filepath) { "foo#{content}bar" }
end
@@ -28,7 +23,7 @@ module Producer::Core
context 'when file does not contain the content' do
before do
allow(fs).to receive(:file_read).with(filepath) { 'foo bar' }
allow(remote_fs).to receive(:file_read).with(filepath) { 'foo bar' }
end
it 'returns false' do
@@ -37,7 +32,9 @@ module Producer::Core
end
context 'when file does not exist' do
before { allow(fs).to receive(:file_read).with(filepath) { nil } }
before do
allow(remote_fs).to receive(:file_read).with(filepath) { nil }
end
it 'returns false' do
expect(test.verify).to be false

View File

@@ -2,24 +2,21 @@ require 'spec_helper'
module Producer::Core
module Tests
describe HasDir do
let(:env) { Env.new }
describe HasDir, :env do
let(:path) { 'some_directory' }
subject(:has_dir) { HasDir.new(env, path) }
it_behaves_like 'test'
describe '#verify', :ssh do
before { sftp_story }
describe '#verify' do
it 'delegates the call on remote FS' do
expect(env.remote.fs).to receive(:dir?).with(path)
expect(remote_fs).to receive(:dir?).with(path)
has_dir.verify
end
it 'returns the dir existence' do
existence = double 'existence'
allow(env.remote.fs).to receive(:dir?) { existence }
allow(remote_fs).to receive(:dir?) { existence }
expect(has_dir.verify).to be existence
end
end

View File

@@ -2,24 +2,21 @@ require 'spec_helper'
module Producer::Core
module Tests
describe HasFile do
let(:env) { Env.new }
describe HasFile, :env do
let(:filepath) { 'some_file' }
subject(:has_file) { HasFile.new(env, filepath) }
it_behaves_like 'test'
describe '#verify', :ssh do
before { sftp_story }
describe '#verify' do
it 'delegates the call on remote FS' do
expect(env.remote.fs).to receive(:file?).with(filepath)
expect(remote_fs).to receive(:file?).with(filepath)
has_file.verify
end
it 'returns the file existence' do
existence = double 'existence'
allow(env.remote.fs).to receive(:file?) { existence }
allow(remote_fs).to receive(:file?) { existence }
expect(has_file.verify).to be existence
end
end