Using Rspec DescribeMethod
Using Rspec DescribeMethod is super simple.
In your Gemfile:
group :development, :testing do
gem 'rspec-describe-method'
end
In your spec_helper.rb:
require 'rspec/describe-method'
And your specs can look like this:
describe String do
describe_method '.new' do
it{ should be_a String }
end
describe_method '#concat', 'argument' do
it{ should match /argument/i }
end
context 'with an instance' do
subject{ String.new 'test' }
describe_method '#upcase' do
it{ should eq 'TEST' }
end
end
end
Describe a method call on the current test subject
in your specs with 'describe_method', and a #
for instance methods and a .
for class methods.
Instances will automatically delagate to their class, and classes will automatically create an instance of themselves.
Alias method: when_calling
The alias method when_calling
is provided which makes specs more human-readable, especially when calls are nested within eachother:
describe String do
subject{ String.new 'test' }
when_calling '#upcase' {
it{ should eq 'TEST' }
when_calling '#+', 'ING' {
it{ should eq 'TESTING' }
}
}
end
NOTE
When the subject is a Class, and you call an instance method describe_method "#something"
the class will call .new
on itself with NO ARGUMENTS, you may not want this behavior.