This is a library to aid in the usage of fakes when using RSpec. It adds a bunch of convienience methods and matchers to aid in the usage of the heavily AAA style isolation library.
Installation
Installing - install.sh
1
gem install fakes-rspec
Or (preferably using bundler), in your gemfile:
Bundler - gemfile.rb
12
source"http://rubygems.org"gem'fakes-rspec'
When you install the gem it will install the rspec gem also, so you will immediately ready to go.
Usage
Creating a fake
Using a let block
Let Initialization - let.rb
123
describe"Some Feature"dolet(:my_fake){fake}end
Inline
Inline Initialization - inline.rb
12345
describe"Some Feature"doit"should be able to create a fake"doitem=fakeendend
Configuring a fake with return values for calls
Irrespective of arguments:
Faking Return Values Regardless Of Arguments - code.rb
1234567
it"should be able to setup a fakes return values"dothe_fake=fakefake.stub(:hello).and_return("World")fake.hello.should=="World"fake.hello("There").should=="World"end
Caring about arguments:
Faking Return Values With Specific Sets Of Arguments - specific_arguments.rb
1234567891011
it"should be able to setup a fakes return values"dothe_fake=fakefake.stub(:hello).with("There").and_return("World")fake.stub(:hello).with("You").and_return("Again")fake.stub(:hello).and_return("Does Not Matter")# when you use the catch_all, make sure that it is the last step used for a particular method (as above)fake.hello("There").should=="World"fake.hello("You").should=="Again"fake.hello.should=="Does Not Matter"end
Determining whether a call was made
One of the big strengths of this library compared to some of the other ruby isolation libraries is the ability to let you make assertions against the fake after the [subject] has run its code. The following examples demonstrate some typical usage scenarios:
Irrespective of arguments:
Verifying Call Made Ignoring Arguments - call_verify_ignoring_arguments.rb
123456
it"should be able to determine if a call was made on a fake"dothe_fake=fakefake.hello("World")fake.shouldhave_received(:hello)#trueend
With a specific set of arguments:
Verifying Calls Made With Specific Arguments - verifying_calls_made_with_specific_arguments.rb
1234567
it"should be able to determine if a call was made on a fake"dothe_fake=fakefake.hello("World")fake.shouldhave_received(:hello,"World")#truefake.shouldhave_received(:hello,"Other")#falseend
Remember, that because it is just a matcher, to negate the matcher you can use the should_not qualifier to do the opposite:
Determine whether a call was not made with a specific set of arguments:
Verifying A Call Was Not Made With A Specific Set Of Arguments - not_made_with_specific_arguments.rb
123456
it"should be able to determine if a call was not made on a fake"dothe_fake=fakefake.hello("World")fake.should_nothave_received(:hello,"Other")#trueend
Determining that a call was made a certain number of times
Irrespective of arguments:
Verifying Occurences Of A Call Ignoring Arguments - verifying_occurences_ignoring_arguments.rb
123456
it"should be able to determine if a call was made on a fake"dothe_fake=fakefake.hello("World")fake.shouldhave_received(:hello).once#trueend
Caring about arguments:
Verifying Occurences Of A Call Including Arguments - verifying_occurences_including_arguments.rb
1234567
it"should be able to determine if a call was made on a fake"dothe_fake=fakefake.hello("World")fake.shouldhave_received(:hello,"World").once#truefake.shouldhave_received(:hello,"Earth").once#falseend
Remember, that because it is just a matcher, to negate the matcher you can use the should_not qualifier to do the opposite:
Determine whether a call was not made a specific number of times with a specific set of arguments:
1234567
it"should be able to determine if a call was made on a fake"dothe_fake=fakefake.hello("World")fake.should_nothave_received(:hello,"World").twice#truefake.should_nothave_received(:hello).twice#trueend
After calling have_received, you can specify occurences using one of the following methods:
once
twice
at_least_once
at_least_twice
at_most_once
at_most_twice
at_least(times)
at_most(times)
exactly(times)
occurs(match_block) - Where match_block is a proc/lambda that matches the signature lambda{|number| bool}
An example of using the occurs method would be as follows:
Determine whether a call was not made between a certain number of times
Verifying Calls Using Occurs - occurs.rb
1234567
it"should be able to determine if a call was made on a fake"dothe_fake=fakefake.hello("World")fake.hello("Again")fake.shouldhave_received(:hello).occurs(lambda{|number|(1..3)===number})#trueend
Contributing
Feel free to fork this codebase and submit any pull requests you think would be useful.