Project

spies

0.0
No commit activity in last 3 years
No release in over 3 years
Ruby test spies
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
>= 4.7.2, ~> 4.7
~> 10.0
~> 3.0
~> 0.41.2
>= 0
 Project Readme

Ruby Spy Build Status Coverage Status Yard Docs

Test spies for Ruby. Why? For fun.

Installation

gem 'spies', require: 'spy'

Usage

Spy on all methods of an instance:

spy = Spy.on(my_object)
spy.some_method
expect(spy.calls.count).to eq 1
expect(spy.calls.last.method_name).to eq :some_method

Spy on arguments:

spy = Spy.on(my_object)

spy.some_method_with_args('foo', 'bar')
expect(spy.calls.last.args).to eq %w(foo bar)

block = -> {}
spy.some_method_with_block(&block)
expect(spy.calls.last.block).to eq block

Spy on one method:

spy = Spy.on(my_object, :some_method)
spy.some_method
spy.another_method
expect(spy.calls.count).to eq 1

Spy on a constant:

spy = Spy.on(SomeClass)
SomeClass.some_method
expect(spy.calls.count).to eq 1

Spy on all instances of a class:

spy = Spy.on_all_instances_of(SomeClass)

instance_1 = SomeClass.new
instance_1.some_method

instance_2 = SomeClass.new
instance_2.some_method

expect(spy.calls.count).to eq 2
expect(spy.calls[1].receiver).to eq instance_2

Clean up after yourself:

Spy.clean

Or keep a block clean:

Spy.clean do
  Spy.on(my_object)
end

Cleaning with RSpec:

before(:each) { Spy.clean }