expectations
expectations is a lightweight unit testing framework. Tests (expectations) can be written as follows
expect 2 do
1 + 1
end
expect NoMethodError do
Object.invalid_method_call
end.
expectations is designed to encourage unit testing best practices such as
- discourage setting more than one expectation at a time
- promote maintainability by not providing a setup or teardown method
- provide one syntax for setting up state based or behavior based expectation
- focus on readability by providing no mechanism for describing an expectation other than the code in the expectation.
Mocking is done utilizing Mocha
Download and Installation
You can download expectations from here or install it with the following command.
$ gem install expectations
License
You may use, copy and redistribute this library under the same terms as Ruby itself (see www.ruby-lang.org/en/LICENSE.txt).
TextMate
The following code can be used as a new command in TextMate for running an individual expectation.
export LINE="$TM_LINE_NUMBER"
export RUBYLIB="$TM_BUNDLE_SUPPORT/RubyMate${RUBYLIB:+:$RUBYLIB}"
"${TM_RUBY:-ruby}" -- "$TM_BUNDLE_SUPPORT/RubyMate/run_script.rb"
Usage
expectations can be used for state based and behavior based testing.
require File.dirname(__FILE__) + “/test_helper” Expectations do
# State based expectation where a value equals another value expect 2 do 1 + 1 end
# State based expectation where an exception is expected. Simply expect the Class of the intended exception expect NoMethodError do Object.no_method end
# Behavior based test using a traditional mock expect mock.to_receive(:dial).with(“2125551212”).times(2) do |phone| phone.dial(“2125551212”) phone.dial(“2125551212”) end # Behavior based test on a concrete mock expect Object.to_receive(:deal) do Object.deal end
# State based test utilizing a stub expect 2 do stub(:two => 2).two end
# State based test utilizing a concrete mock expect 2 do Object.expects(:bar).returns 2 Object.bar end
# Behavior based test utilizing a stub and a concrete mock expect 1 do Object.expects(:give_me_three).with(3).returns 1 Object.give_me_three(stub(:three=>3).three) end
end
Contributors
Matt Mower