Rails 3应用程序正常运行,集成测试失败:如何解决Cookie问题? [英] Rails 3 app works, integration test fails: how to fix cookie issue?
问题描述
我有一个简单的Rails 3应用程序:没有模型,一个视图和一个具有单个index
方法的控制器,该方法会生成静态内容.效果很好:当我手动浏览到'/'
时,我看到了我的静态内容.
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
通过rake
运行此测试时,它告诉我GET '/'
的响应为500.检查日志输出会在响应正文中显示以下消息:
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
当然secret_token.rb
存在并且包含正确格式的机密(它是由Rails 3本身生成的.)
Of course secret_token.rb
exists and contains a properly formatted secret (it was generated by Rails 3 itself.)
任何人都知道如何在Rails 3中启用集成测试吗?我猜想也许我需要设置一些其他配置选项来告诉我的应用接受集成测试.
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.
推荐答案
您需要包括Rails生成的文件test_helper.rb
.这包含进行集成测试所必需的魔术.
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' )
上面的路径'../test_helper.rb'
应该相对于包含它的测试文件.
The path '../test_helper.rb'
above should be relative to the test file that includes it.
仅供参考:上面是一个Ruby惯用法,用于通过相对路径(如C中的#include "../foo.h"
)包含文件.如果您使用的是Ruby 1.9.x,则可以使用一种更简单的形式:
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'
这篇关于Rails 3应用程序正常运行,集成测试失败:如何解决Cookie问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!