On 30-Mar-08, at 13:01 , Phlip wrote:
> Merbies:
>
> I have clawed my way to the next line in my spec:


I've got a pair of spec helpers that function in current (0.9.2) merb,  
for doing view and controller specs separately.

def describe_action(controller, action, &block)
   describe "#{controller}##{action}" do
     before(:each) do
       @controller_class, @action = controller, action
       @session = {}
     end

     def dispatch(params = {}, env = {})
       if obj = instance_variable_get(
                     "@[EMAIL PROTECTED]")
         params = {:id => obj.id}.merge(params)
       end

       @controller_class.any_instance.stubs(:session).returns(@session)
       @controller_class.any_instance.stubs(:render)
       @controller = dispatch_to(@controller_class, @action, params,  
env)

       if obj
         obj.reload unless obj.new_record?
       end
     end
     def post_dispatch(params = {}, env = {})
       dispatch(params, env.merge(:REQUEST_METHOD => 'POST'))
     end

     instance_eval &block
   end
end

def describe_view(controller, template, &block)
   describe "/#{controller.to_s.downcase}/#{template}" do
     before(:each) do
       @controller_class, @template = controller, template
       @assigns = {}
     end

     def render(render_opts={})
       controller = @controller_class.new(fake_request)
       @assigns.each do |key, value|
         controller.instance_variable_set("@#{key}", value)
       end
       if @template.to_s =~ /^_/
         template = @template.to_s[1..-1].to_sym # strip leading _
         @content = controller.partial(template, render_opts)
       else
         @content = controller.render(@template.to_sym, render_opts)
       end
     end

     instance_eval &block
   end
end

> require File.join(File.dirname(__FILE__), "..", 'spec_helper.rb')
> require File.dirname(__FILE__) + '/../mock_request'
>
> describe Grepper, "index action" do
>  before :each do
>    @in = Merb::MockRequest.new # ?
>    @res = StringIO.new  # ?
>    @controller = Merb::Controller.new(:controller => 'Grepper',
>                                           :action => 'index')
>  end
>
>  it 'should do something' do
> #   @controller.dispatch(:index)
>  end
> end

You could probably get away with your example being:

require File.join(File.dirname(__FILE__), "..", 'spec_helper.rb')

describe_action Grepper, :index do
   it 'should do something' do
     dispatch
   end
end


I really should get around to blogging them - those helpers are  
bouncing around between two apps right now, and they've mostly stopped  
getting tweaks.

-- 
Jamie

_______________________________________________
Merb-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/merb-devel

Reply via email to