Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
/ stubble Public archive

Tools to make stubbing Ruby ORM models just a little bit easier

Notifications You must be signed in to change notification settings

dchelimsky/stubble

Repository files navigation

stubble

DESCRIPTION:

Shave a few lines, but leave a little stubble. Stubble makes stubbing ORM models in rspec controller specs (or rails functional tests) just a little bit easier.

FEATURES/PROBLEMS:

  • Simple means of stubbing multiple ActiveRecord class and instance methods

  • Currently unreleased and somewhat experimental

    • API and underlying functionality are subject to change

      • Especially while it’s not yet released

  • Only works with RSpec’s HEAD (see github.com/dchelimsky/rspec)

    • I’ll release 0.0.1 of stubble after the rspec-1.2.7 release

  • Works with RSpec or Test::Unit and multiple mock frameworks:

    • RSpec’s mocking framework (RSpec only)

    • flexmock

    • mocha

    • rr

SYNOPSIS:

describe ThingsController do
  context "PUT with valid attributes"
    it "redirects to the things index" do
      stubbing(Thing) do
        put :update
        response.should redirect_to(things_path)
      end
    end
  end
end

CONFIGURATION:

RSpec

in spec_helper:

require 'stubble'
Stubble.configure {|c| c.stub_with(:rspec)}
Spec::Runner.configure {|c| c.include(Stubble)}

If you want to use any other mock framework (:mocha, :flexmock or :rr - example below with :rr):

require 'stubble'
Stubble.configure {|c| c.stub_with(:rr)}
Spec::Runner.configure do |c|
  c.include(Stubble)
  c.mock_with(:rr)
end

Test::Unit

in test_helper.rb

require 'stubble'
Stubble.configure {|c| c.stub_with(:mocha)}

class ActiveSupport::TestCase
  include Stubble
end

Same setup for :flexmock and :rr. Does not currently work with rspec’s mock framework

USAGE:

By default, stubbled model classes are happy. They can be found, created, and saved in all number of ways without complaint.

stubbing(Thing) do |thing|
  ...
end

… stubs all the following:

Thing.new                 => thing
Thing.find(id)            => thing
Thing.find(:first)        => thing
Thing.find(:first, *args) => thing
Thing.create              => thing
Thing.create!             => thing

Thing.all                 => [thing]
Thing.all(*args)          => [thing]
Thing.find(:all)          => [thing]
Thing.find(:all, *args)   => [thing]

The instance of Thing is set up with the following stubs:

thing.valid?             => true
thing.save               => true
thing.save!              => true
thing.update_attribute   => true
thing.update_attributes  => true
thing.update_attributes! => true

You can also tell stubble to create an invalid stub, like so:

stubbing(Thing, :as => :invalid) do |thing|
  ...
end

This will respond to various finders, creators just like the default, with the exception of create!

Thing.create! => raise ActiveRecord::RecordInvalid

The instance returned is stubbed as follows:

thing.valid?             => false
thing.save               => false
thing.save!              => raise ActiveRecord::RecordInvalid
thing.update_attribute   => false
thing.update_attributes  => false
thing.update_attributes! => raise ActiveRecord::RecordInvalid

Examples

GET requests

The following examples will both pass if the controller’s index action uses:

  • find(:all)

  • all

    describe "things" do
      context "successful GET"
        it "assigns things" do
          stubbing(Thing) do |thing|
            get :index
            assigns[:things].should == [thing]
          end
        end
      end
    end

POST requests

The following examples will both pass if the controller’s index action uses:

  • new

  • create

  • create!

  • valid?

  • save

  • save!

    describe "things" do
      context "successful POST"
        it "creates a new thing" do
          stubbing(Thing) do |thing|
            post :create
            assigns[:things].should == [thing]
          end
        end
      end
    end

PUT requests

The following examples will both pass if the controller’s update action uses find with any of:

  • update_attribute + (save || save!)

  • update_attributes

  • update_attributes!

    describe "things" do
      context "successful PUT"
        it "redirects to the things index" do
          stubbing(Thing) do
            put :update
            response.should redirect_to(things_path)
          end
        end
      end
    
      context "failed PUT"
        it "re-renders the edit page" do
          stubbing(Thing, :as => :invalid) do
            put :update
            response.should render_template('edit')
          end
        end
      end
    end

REQUIREMENTS:

  • activerecord

  • rspec

INSTALL:

$ git clone git://github.com/dchelimsky/stubble.git
$ cd stubble
$ rake gem
$ rake install_gem

CONFIGURE

in spec_helper.rb

require 'stubble'
Stubble.configure {|stubble| stubble.stub_with :rspec} # or mocha, rr, flexmock
Spec::Runner.configure {|c| c.include(Stubble, :type => :controller)}

in test_helper.rb # if you’re using test/unit or any of its extensions

require 'stubble'
Stubble.configure {|stubble| stubble.stub_with :mocha} # or rr, flexmock
class ActionController::TestCase
  include Stubble
end

LICENSE:

(The MIT License)

Copyright © 2008-2009 David Chelimsky

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Tools to make stubbing Ruby ORM models just a little bit easier

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages