Skip to content

dcadenas/stubborn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stubborn

A Ruby gem that helps you find missing stubs in your tests.

Usage

Let’s explain through an example. Say you have this classes:

class Api
  def slow_method(a, b, c)
    ...
    #this is slow, dangerous, or it's just an external dependency that should be stubbed
    ...
  end
end

class MyClassThatUsesApi
  def useful_method
    ...
    important_value = api.slow_method(important_parameter, something_else, another)
    ...
  end
end

And you’ve forgot to stub your api class in many tests of your huge test suite:

it "should do something useful" do
  MyClassThatUsesApi.new.useful_method.should be_useful
end

Then you could use stubborn to help you find and prevent those misses just by adding this somewhere in your test_helper/spec_helper:

require 'rubygems'
require 'stubborn'

Stubborn.should_be_stubbed(Api)

Now each test with a missing stub will fail throwing an exception that includes all information you need to construct the correct stub:

You've missed adding a stub. Consider this suggestions:
api_instance.stub!(:slow_method).with(123, AClass, "hi").and_return(another_class_instance)
api_instance.stub!(:slow_method).and_return(another_class_instance)

You have some control on the output by using the :label option so you can better match the way you usually call the methods from your test:

Stubborn.should_be_stubbed(Api, :label => "GreatClass.singleton")

Which produces this output:

You've missed adding a stub. Consider this suggestions:
GreatClass.singleton.stub!(:slow_method).with(123, AClass, "hi").and_return(another_class_instance)
GreatClass.singleton.stub!(:slow_method).and_return(another_class_instance)

So all unstubbed class methods of the Api class and its instances will raise this special exception while running the tests. If you prefer you can use it on a single instance like this:

api = Stubborn.should_be_stubbed(Api.new)

Filters

You can control which methods to check with the options :except and :only. Both accept either strings or arrays:

Stubborn.should_be_stubbed(Api.new, :except => :safe_method)
Stubborn.should_be_stubbed(Api.new, :only => [:slow_method, :external_service])

You can also filter on instance methods. All you need to do is using the :instance_methods key to scope your :only or :except methods like this:

Stubborn.should_be_stubbed(User, :instance_methods => {:only => :save})

Installation

sudo gem install stubborn

To Do

  • Give stub suggestions for more test frameworks. It currently shows only rspec syntax

Collaborate

github.com/dcadenas/stubborn

Copyright © 2009 Daniel Cadenas. See LICENSE for details.

About

A Ruby gem that helps you find missing stubs in your tests.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages