Using RSpec Mocks outside of tests

When I started mocking objects using RSpec Mocks, I never thought I’d end up using them outside of actual unit testing.

But this week, while working on a new internal gem to build out how we register documents, I found myself wanting to get some early acceptance testing from the end users. To do this I build a throw-away Sinatra application that took some user input. I then used this input the mock the return values for one of the methods in my actual class instance. This allowed my users to test all sorts of different scenarios, even ones I may have missed in my unit tests.

To use this yourself, you just need to include the following line to use the RSpec Mock standalone in your application:

require 'rspec/mocks/standalone'

Then just use mock your instance as before:


#stub method . . .
obj = MyObject.new
obj.stub(:method_name => params[:key]) #user-entered values returned

#. . . and call
obj.method_name

You can find more details here.

Advertisements
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s