jwfearn
jwfearn

Reputation: 29617

Rails 3 app works, integration test fails: how to fix cookie issue?

I have a simple Rails 3 app: no models, one view and one controller with a single index method which produces static content. It works fine: when I manually browse to '/' I see my static content.

Then I added an integration test:

# myproj/test/integration/routes_test.rb
class RoutesTest < ActionController::IntegrationTest
  test 'default_route' do
    get '/'
    assert_response :success, @response.body
  end
end

When running this test via rake, it tells me that GET '/' is responding with 500. Examination of the log output reveals this message in the response body:

A secret is required to generate an integrity hash for cookie session
data. Use config.secret_token = "some secret phrase of at least 30
characters" in config/initializers/secret_token.rb

Of course secret_token.rb exists and contains a properly formatted secret (it was generated by Rails 3 itself.)

Anyone out there know how to enable integration testing in Rails 3? I'm guessing perhaps I need to set some other configuration options to tell my app to accept integration tests.

Upvotes: 0

Views: 770

Answers (1)

jwfearn
jwfearn

Reputation: 29617

You need to include the Rails-generated file test_helper.rb. This contains the magic necessary to allow integration testing.

In your test file(s), prepend the line:

require File.join( File.dirname( __FILE__ ), '../test_helper.rb' )

The path '../test_helper.rb' above should be relative to the test file that includes it.

FYI: The above is a Ruby idiom for including files by with relative paths (like #include "../foo.h" in C.) If you're using Ruby 1.9.x, you can use a simpler form:

require_relative '../test_helper.rb'

Upvotes: 1

Related Questions