[Rspec-devel] New proc.should_increment method

aslak hellesoy aslak.hellesoy at gmail.com
Fri Jul 14 19:50:14 EDT 2006


On 7/14/06, Lachie <lachiec at gmail.com> wrote:
> On 7/15/06, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:
> > On 7/14/06, David Chelimsky <dchelimsky at gmail.com> wrote:
> > > > On 7/13/06, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:
> > > > I'm all for removing the feature from RSpec - it's easy enough for me
> > > > to monkey patch ShouldHelper in my own project to add
> > > > should_increment.
> > >
> > > Is it in a rails app? If so, why not make it a plugin?
> >
> > I will
>
> I'd love to see this as an example of adding bespoke expectations from
> rails plugins, as well as in more general apps.

Right now I'm achieving this by simply monkey patching RSpec. Stick
this in vendor/plugins/rspec_ext/init.rb:

require 'spec'

module Spec
  class ShouldHelper < ShouldBase
    def increment(object, method, difference=1)
      initial_value = object.__send__(method)
      @target.call
      object.__send__(method).should.equal(initial_value + difference)
    end

    def decrement(object, method, difference=1)
      increment(object, method, -difference)
    end
  end

  class ShouldNegator < ShouldBase
    def change(object, method)
      initial_value = object.__send__(method)
      @target.call
      object.__send__(method).should.equal(initial_value)
    end
  end
end

This is the recently reverted code that allows you to do:

    arr = ["something"]
    lambda { arr.pop }.should.decrement arr, :length

-Which is not BDD, but still useful in Rails while waiting for better
mock support in rails.

Aslak

> _______________________________________________
> Rspec-devel mailing list
> Rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel
>


More information about the Rspec-devel mailing list