Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .document
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 stubborn.gemspec
README.rdoc

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

Copyright © 2009 Daniel Cadenas. See LICENSE for details.

Something went wrong with that request. Please try again.