Permalink
Browse files

Use current RSpec idioms

* `double` instead of `mock`
* `expect ... to` instead of `should`/`should_receive`
* `allow ... to receive` instead of `stub!`
* Named subjects and classes/modules
* Present tense instead of deontic modality :-)
  • Loading branch information...
1 parent 3a75d98 commit 0e916582d30c9b6ad18ef2196eb2a0fc24eeda4d @njonsson committed Nov 9, 2013
@@ -3,13 +3,23 @@
require 'cape/rake'
describe Cape::Capistrano do
+ subject(:capistrano) { capistrano_class.new }
+
+ let(:capistrano_class) { described_class }
+
describe 'without specified attributes' do
- its(:rake) { should == Cape::Rake.new }
+ describe '#rake' do
+ specify { expect(capistrano.rake).to eq(Cape::Rake.new) }
+ end
end
describe 'with specified attributes' do
- subject { described_class.new :rake => 'the specified value of #rake' }
+ subject(:capistrano) {
+ capistrano_class.new :rake => 'the specified value of #rake'
+ }
- its(:rake) { should == 'the specified value of #rake' }
+ describe '#rake' do
+ specify { expect(capistrano.rake).to eq('the specified value of #rake') }
+ end
end
end
@@ -2,12 +2,12 @@
require 'cape/core_ext/hash'
describe Hash do
- subject { {:foo => 'bar', :baz => 'qux', :quux => 'corge'} }
+ subject(:hash) { {:foo => 'bar', :baz => 'qux', :quux => 'corge'} }
- describe '-- when sent #slice with keys that are present and those that are not --' do
- it 'should return the expected subset hash' do
- expect(subject.slice(:baz, :fizzle, :quux)).to eq(:baz => 'qux',
- :quux => 'corge')
+ describe '#slice with keys that are present and those that are not' do
+ it 'returns the expected subset hash' do
+ expect(hash.slice(:baz, :fizzle, :quux)).to eq(:baz => 'qux',
+ :quux => 'corge')
end
end
end
@@ -2,15 +2,17 @@
require 'cape/core_ext/symbol'
describe Symbol do
- it '-- when sent #<=> with a lower symbol -- should return 1' do
- expect(:foo <=> :bar).to eq(1)
- end
+ describe '#<=>' do
+ describe 'with a lower symbol' do
+ specify { expect(:foo <=> :bar).to eq(1) }
+ end
- it '-- when sent #<=> with a higher symbol -- should return -1' do
- expect(:baz <=> :qux).to eq(-1)
- end
+ describe 'with a higher symbol' do
+ specify { expect(:baz <=> :qux).to eq(-1) }
+ end
- it '-- when sent #<=> with itself -- should return 0' do
- expect(:quux <=> :quux).to eq(0)
+ describe 'with itself' do
+ specify { expect(:quux <=> :quux).to eq(0) }
+ end
end
end
View
@@ -6,50 +6,52 @@
require 'cape/rake'
describe Cape::DSL do
- subject do
+ subject(:dsl) {
Object.new.tap do |o|
- o.extend described_class
+ o.extend dsl_module
end
- end
+ }
+
+ let(:dsl_module) { described_class }
- let(:mock_capistrano) { mock(Cape::Capistrano).as_null_object }
+ let(:capistrano) { double(Cape::Capistrano).as_null_object }
- let(:mock_rake) { mock(Cape::Rake).as_null_object }
+ let(:rake) { double(Cape::Rake).as_null_object }
let(:task_expression) { 'task:expression' }
before :each do
- Cape::Capistrano.stub!(:new).and_return mock_capistrano
- Cape::Rake. stub!(:new).and_return mock_rake
+ allow(Cape::Capistrano).to receive(:new).and_return(capistrano)
+ allow(Cape::Rake ).to receive(:new).and_return(rake)
end
- describe '-- when sent #each_rake_task --' do
+ describe '#each_rake_task' do
def do_each_rake_task(&block)
- subject.each_rake_task(task_expression, &block)
+ dsl.each_rake_task(task_expression, &block)
end
- it 'should delegate to Rake#each_task' do
- mock_rake.should_receive(:each_task).
- with(task_expression).
- and_yield({:name => task_expression})
+ it 'delegates to Rake#each_task' do
+ expect(rake).to receive(:each_task).
+ with(task_expression).
+ and_yield({:name => task_expression})
do_each_rake_task do |t|
expect(t).to eq(:name => task_expression)
end
end
- it 'should return itself' do
- expect(do_each_rake_task).to eq(subject)
+ it 'returns itself' do
+ expect(do_each_rake_task).to eq(dsl)
end
end
- describe '-- when sent #mirror_rake_tasks' do
+ describe '#mirror_rake_tasks' do
before :each do
- mock_rake.stub!(:each_task).and_yield({:name => task_expression})
- subject.stub! :raise_unless_capistrano
+ allow(rake).to receive(:each_task).and_yield({:name => task_expression})
+ allow(dsl ).to receive(:raise_unless_capistrano)
end
def do_mirror_rake_tasks(*arguments, &block)
- subject.mirror_rake_tasks(*arguments, &block)
+ dsl.mirror_rake_tasks(*arguments, &block)
end
describe 'with two scalar arguments --' do
@@ -63,51 +65,43 @@ def do_mirror_rake_tasks(*arguments, &block)
shared_examples_for "a successful call (#{Cape::DSL.name})" do |task_expression_in_use,
options_in_use|
specify 'by collecting Rake#each_task' do
- mock_rake.should_receive(:each_task).with task_expression_in_use
+ expect(rake).to receive(:each_task).with(task_expression_in_use)
do_mirror_rake_tasks
end
specify 'by verifying that Capistrano is in use' do
- subject.should_receive :raise_unless_capistrano
+ expect(dsl).to receive(:raise_unless_capistrano)
do_mirror_rake_tasks
end
describe 'by sending Capistrano#define_rake_wrapper for Rake#each_task' do
specify 'with the expected task' do
- mock_capistrano.should_receive(:define_rake_wrapper).with do |task,
- named_arguments|
- task == {:name => task_expression}
- end
+ expect(capistrano).to receive(:define_rake_wrapper).
+ with { |task, named_arguments| task == {:name => task_expression} }
do_mirror_rake_tasks
end
specify 'with the expected named options' do
- mock_capistrano.should_receive(:define_rake_wrapper).with do |task,
- named_arguments|
- named_arguments.keys.sort == ([:binding] + options_in_use.keys).sort
- end
+ expect(capistrano).to receive(:define_rake_wrapper).
+ with { |task, named_arguments| named_arguments.keys.sort == ([:binding] + options_in_use.keys).sort }
do_mirror_rake_tasks
end
specify 'with a :binding option' do
- mock_capistrano.should_receive(:define_rake_wrapper).with do |task,
- named_arguments|
- named_arguments[:binding].is_a? Binding
- end
+ expect(capistrano).to receive(:define_rake_wrapper).
+ with { |task, named_arguments| named_arguments[:binding].is_a? Binding }
do_mirror_rake_tasks
end
specify 'with any provided extra options' do
- mock_capistrano.should_receive(:define_rake_wrapper).with do |task,
- named_arguments|
- named_arguments.slice(*options_in_use.keys) == options_in_use
- end
+ expect(capistrano).to receive(:define_rake_wrapper).
+ with { |task, named_arguments| named_arguments.slice(*options_in_use.keys) == options_in_use }
do_mirror_rake_tasks
end
end
specify 'by returning itself' do
- expect(do_mirror_rake_tasks).to eq(subject)
+ expect(do_mirror_rake_tasks).to eq(dsl)
end
end
@@ -118,57 +112,57 @@ def do_mirror_rake_tasks
end
end
- should_behave_like("a successful call (#{Cape::DSL.name})",
- 'task:expression',
- {})
+ behaves_like("a successful call (#{Cape::DSL.name})",
+ 'task:expression',
+ {})
end
describe 'with one scalar argument --' do
def do_mirror_rake_tasks
super 'task:expression'
end
- should_behave_like("a successful call (#{Cape::DSL.name})",
- 'task:expression',
- {})
+ behaves_like("a successful call (#{Cape::DSL.name})",
+ 'task:expression',
+ {})
end
describe 'without arguments --' do
def do_mirror_rake_tasks
super
end
- should_behave_like("a successful call (#{Cape::DSL.name})", nil, {})
+ behaves_like("a successful call (#{Cape::DSL.name})", nil, {})
end
end
- describe '-- when sent #local_rake_executable --' do
+ describe '#local_rake_executable' do
before :each do
- mock_rake.stub!(:local_executable).and_return 'foo'
+ allow(rake).to receive(:local_executable).and_return('foo')
end
- it 'should delegate to Rake#local_executable' do
- mock_rake.should_receive :local_executable
- subject.local_rake_executable
+ it 'delegates to Rake#local_executable' do
+ expect(rake).to receive(:local_executable)
+ dsl.local_rake_executable
end
- it 'should return the result of Rake#local_executable' do
- expect(subject.local_rake_executable).to eq('foo')
+ it 'returns the result of Rake#local_executable' do
+ expect(dsl.local_rake_executable).to eq('foo')
end
end
- describe '-- when sent #remote_rake_executable --' do
+ describe '#remote_rake_executable' do
before :each do
- mock_rake.stub!(:remote_executable).and_return 'foo'
+ allow(rake).to receive(:remote_executable).and_return('foo')
end
- it 'should delegate to Rake#remote_executable' do
- mock_rake.should_receive :remote_executable
- subject.remote_rake_executable
+ it 'delegates to Rake#remote_executable' do
+ expect(rake).to receive(:remote_executable)
+ dsl.remote_rake_executable
end
- it 'should return the result of Rake#remote_executable' do
- expect(subject.remote_rake_executable).to eq('foo')
+ it 'returns the result of Rake#remote_executable' do
+ expect(dsl.remote_rake_executable).to eq('foo')
end
end
end
@@ -2,49 +2,69 @@
require 'cape/hash_list'
describe Cape::HashList do
+ subject(:hash_list) { hash_list_class.new }
+
+ let(:hash_list_class) { described_class }
+
describe 'that is empty' do
- it { should be_empty }
+ specify { expect(hash_list).to be_empty }
- its(:inspect) { should == '{}' }
+ describe '#inspect' do
+ specify { expect(hash_list.inspect).to eq('{}') }
+ end
- its(:to_a) { should == [] }
+ describe '#to_a' do
+ specify { expect(hash_list.to_a).to eq([]) }
+ end
- its(:to_hash) { should == {} }
+ describe '#to_hash' do
+ specify { expect(hash_list.to_hash).to eq({}) }
+ end
describe 'when values are added out of order' do
before :each do
- subject['foo'] = 'xxx'
- subject['foo'] = 'bar'
- subject['baz'] = 'qux'
+ hash_list['foo'] = 'xxx'
+ hash_list['foo'] = 'bar'
+ hash_list['baz'] = 'qux'
end
- it { should == {'foo' => 'bar', 'baz' => 'qux'} }
+ specify { expect(hash_list).to eq({'foo' => 'bar', 'baz' => 'qux'}) }
- its(:inspect) { should == '{"foo"=>"bar", "baz"=>"qux"}' }
+ describe '#inspect' do
+ specify {
+ expect(hash_list.inspect).to eq('{"foo"=>"bar", "baz"=>"qux"}')
+ }
+ end
- its(:to_a) { should == [%w(foo bar), %w(baz qux)] }
+ describe '#to_a' do
+ specify { expect(hash_list.to_a).to eq([%w(foo bar), %w(baz qux)]) }
+ end
- its(:to_hash) { should == {'foo' => 'bar', 'baz' => 'qux'} }
+ describe '#to_hash' do
+ specify {
+ expect(hash_list.to_hash).to eq({'foo' => 'bar', 'baz' => 'qux'})
+ }
+ end
end
end
describe 'that has values out of order' do
- subject { described_class.new 'foo' => 'bar', 'baz' => 'qux' }
+ subject(:hash_list) { hash_list_class.new 'foo' => 'bar', 'baz' => 'qux' }
- it { should == {'foo' => 'bar', 'baz' => 'qux'} }
+ specify { expect(hash_list).to eq({'foo' => 'bar', 'baz' => 'qux'}) }
- it 'should index the values as expected' do
- expect(subject['foo']).to eq('bar')
- expect(subject['baz']).to eq('qux')
- expect(subject['not-found']).to be_nil
+ it 'indexes the values as expected' do
+ expect(hash_list['foo']).to eq('bar')
+ expect(hash_list['baz']).to eq('qux')
+ expect(hash_list['not-found']).to be_nil
end
- describe 'when sent #clear' do
+ describe '#clear' do
before :each do
- subject.clear
+ hash_list.clear
end
- it { should be_empty }
+ specify { expect(hash_list).to be_empty }
end
end
end
Oops, something went wrong.

0 comments on commit 0e91658

Please sign in to comment.