views:

203

answers:

3

I'm writing a sinatra app and testing it with rspec and rack/test (as described on sinatrarb.com).
It's been great so far, until I moved some rather procedural code from my domain objects to sinatra helpers.

Since then, I've been trying to figure out how to test these in isolation ?

A: 

maybe this can help you some way http://japhr.blogspot.com/2009/03/sinatra-innards-deletgator.html

zed_0xff
Indeed, i went that 'route', works well :oPThanks zed.
julien
A: 

Actually you don't need to do:

helpers do
  include FooBar
end

Since you can just call

helpers FooBar

The helpers method takes a list of modules to mix-in and an optional block which is class-eval'd in: http://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L920-923

A: 

I've also tried this (which needs to be cleaned up a bit to be reusable) to isolate each helper in its own environment to be tested:

class SinatraSim
  def initialize
    ...set up object here...
  end
end

def helpers(&block)
  SinatraSim.class_eval(&block)
end

require 'my/helper/definition' # defines my_helper

describe SinatraSim do
  subject { SinatraSim.new(setup) }

  it "should do something"
    subject.expects(:erb).with(:a_template_to_render) # mocha mocking
    subject.my_helper(something).should == "something else"
  end
end
John Bintz