Skip to content
YorickPeterse edited this page Nov 16, 2012 · 5 revisions

Cookbook

FAQ

When I use follow_redirect! I don't get the right page !

This is probably because your controller issues a redirect_referer but the referer isn't set. so you end up on the '/' page.

You can force the referer in the POST request, passing a env hash as the third argument :

post('/album/save', { :artist => 12 }, { 'HTTP_REFERER' => '/album/create' }

When I run my spec directly, I have complaints about helpers !

This is probably because your helpers are not loaded first in your app.rb top file. Change it so the helpers are required first :

require __DIR__('helper/init')
require __DIR__('model/init')
require __DIR__('controller/init')

What does behaves_like means in bacon ?

behaves_like basically tells bacon to use a certain configuration for the describe() block and let you use #get, #post, ... methods.

What to return from a controller for an unacceptable operation ?

For instance, when a user creates a must-be-unique-record that already exists, should I return a 406 , Or a 200 ?

When writing REST service, you probably want to use 4xx codes. But If you write a web application, just return a 200 and explain why it failed.

You can use flash[:error] for this.

How can I run my test with a specific Rackup file (config.ru) ?

The behaves_like :rack_test is very convenient for testing your application behavior, but it doesn't test with your config.ru file which can be a problem if your application uses another Rack application for specific url using the map method :

map "/user" do
  # This is a Grape application
  run UserApi
end

Ramaze.start...

So instead of doing this in your test :

require_relative '../helper'

describe UserApi do
  behaves_like :rack_test

  should...
end

You'll have to specify which Rack Application (created by RackBuilder) to use by overriding Rack::Test.app method :

require_relative '../helper'

describe UserApi do
  extend Rack::Test::Methods

  def app
    Rack::Builder.parse_file("config.ru").first
  end

  should...
end

Hope this helps :)

Clone this wiki locally