Skip to content

Instantly share code, notes, and snippets.

@dkubb
Created November 10, 2008 07:23
Show Gist options
  • Save dkubb/23442 to your computer and use it in GitHub Desktop.
Save dkubb/23442 to your computer and use it in GitHub Desktop.
# Rspec Template
# --------------
# 1) Setup the subject. Only really possible when the same object is being
# used for multiple tests. Sometimes the subject will vary based on
# the describe blocks, in which case the subject block will be defined
# inside it.
subject { @string = '' }
# 2) Specify the class or textual description for non-public classes and the method
describe String, '#<<' do
# 3) Describe the starting object state and arguments
describe 'given an empty string', 'when appending another string' do
# 4) Setup the object state and arguments
before { @other = 'Hello World' }
# 5) Define the action the "it" blocks will test the behavior of
action { subject << @other }
# 6) Check the return value
# - make sure the object class is expected
# - make sure the object value is expected
# - when self is returned, make sure to use the equal_self matcher
should_return_kind_of(String)
should_return_subject
should_return_expected_value { 'Hello World' }
# (ALTERNATE STEP 6 -- when the action causes an exception)
#
# 6) Check the exception
# - make sure the exception class is expected
# - make sure the exception message is expected (only if within our control)
should_raise_error(FooError, 'an error message')
# 7) Check for side effects
# - Check @self to see how if it's changed
# - Check the arguments to see if they've been changed
it 'should not change the other string' do
lambda { action }.should_not change(@other, :to_s)
end
end
end
# concise form without instructional comments:
subject { @string = '' }
describe String, '#<<' do
describe 'given an empty string', 'when appending another string' do
before { @other = 'Hello World' }
action { subject << @other }
should_be_a_kind_of(String)
should_equal_subject
should_return_expected_value { 'Hello World' }
should_raise_error(FooError, 'an error message')
it 'should not change the other string' do
lambda { action }.should_not change(@other, :to_s)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment