RR
RR is a test double framework for Ruby that features a rich selection of double
techniques and a terse syntax.
Learning more
- A whirlwind tour of RR
- What is a test double?
- Syntax between RR and other double/mock frameworks
- API overview - Full listing of DSL methods
A whirlwind tour of RR
Stubs
stub(object).foo
stub(MyClass).foo
stub(object).foo { 'bar' }
stub(MyClass).foo { 'bar' }
stub(object).foo(1, 2) { 'bar' }
stub(MyClass).foo(1, 2) { 'bar' }
Mocks
mock(object).foo
mock(MyClass).foo
mock(object).foo { 'bar' }
mock(MyClass).foo { 'bar' }
mock(object).foo(1, 2) { 'bar' }
mock(MyClass).foo(1, 2) { 'bar' }
Spies
stub(object).foo
expect(object).to have_received.foo
stub(object).foo
assert_received(object) {|o| o.foo }
Proxies
stub.proxy(object).foo {|str| str.upcase }
stub.proxy(MyClass).foo {|str| str.upcase }
mock.proxy(object).foo {|str| str.upcase }
mock.proxy(MyClass).foo {|str| str.upcase }
stub.proxy(MyClass).new {|obj| stub(obj).foo; obj }
mock.proxy(MyClass).new {|obj| stub(obj).foo; obj }
Class instances
any_instance_of(MyClass) do |klass|
stub(klass).foo { 'bar' }
end
stub.proxy(MyClass).new do |obj|
stub(obj).foo { 'bar' }
end
Installing RR into your project
NOTE: If you want to use RR with
test-unit, use
test-unit-rr. You don't
need to read the following subsections.
For minimal setup, RR looks for an existing test framework and then hooks itself
into it. Hence, RR works best when loaded after the test framework that you
are using is loaded.
If you are using Bundler, you can achieve this by specifying the dependency on
RR with require: false
; then, require RR directly following your test
framework.
Here's what this looks like for different kinds of projects:
Ruby project (without Bundler)
require 'your/test/framework'
require 'rr'
Ruby project (with Bundler)
gem 'rr', require: false
require 'your/test/framework'
require 'rr'
Rails project
group :test do
gem 'rr', require: false
end
require File.expand_path('../../config/environment', __FILE__)
require 'your/test/framework'
require 'rr'
Compatibility
RR is designed and tested to work against the following Ruby versions:
- 2.4
- 2.5
- 2.6
- 2.7
- 3.0
- JRuby 1.7.4
as well as the following test frameworks:
Help!
If you have a question or are having trouble, simply post it as an
issue and I'll respond as soon as I can.
Contributing
Want to contribute a bug fix or new feature to RR? Great! Follow these steps:
- Make sure you have a recent Ruby (check the compatibility table above).
- Clone the repo (you probably knew that already).
- Make a new branch off of
master
with a descriptive name. - Work on your patch.
- Run
bundle install
. - Ensure all of the tests pass by running
bundle exec rake
. - If you want to go the extra mile, install the other Ruby versions listed
above in the compatibility table, and repeat steps 5-6. See the "Running test
suites" section below for more information.
- When you're done, push your branch and create a pull request from it.
I'll respond as soon as I can.
Running tests
As indicated by the compatibility list above, in order to test support for
multiple Ruby versions and environments, there are multiple test suites, and
Rake tasks to run these suites. The list of available Rake tasks depends on
which version of Ruby you are under, but you can get the full list with:
bundle exec rake -D spec:
To run all the suites, simply say:
bundle exec rake
(Incidentally, this is also the command which Travis runs.)
Author/Contact
RR was originally written by Brian Takita. And it was maintained by
Elliot Winkler (elliot.winkler@gmail.com). It is currently
maintained by Kouhei Sutou (kou@cozmixng.org).
Credits
With any development effort, there are countless people who have contributed to
making it possible; RR is no exception! You can read the full list of
credits here.
License
RR is available under the MIT license.