我在Rails中使用Mongoid,但生成的所有功能测试都失败了,错误类似于:
test_should_get_new(PostsControllerTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: comments: DELETE FROM "comments"
这些是生成的测试:
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
setup do
@post = posts(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:posts)
end
test "should get new" do
get :new
assert_response :success
end
[...]
end
我应该改变测试吗?或者删除对ActiveRecord或Sqlite的引用?(我的gemfile中仍然有sqlite,因为我在删除它时遇到了问题,并且仍然不确定如何从应用程序中完全删除它,因为我没有使用它做任何事情)
在config/application.rb
中,删除require "rails/all"
并添加
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
在config/development.rb
注释/删除以下内容:
config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
在config/test.rb
注释/删除以下内容:
config.active_record.mass_assignment_sanitizer = :strict
如果不起作用,你能给我看看你的spec_helper.rb
吗?
如果您键入
rails --help
则列出了跳过活动记录的选项
-O, [--skip-active-record] # Skip Active Record files
我建议您使用此选项创建一个新的Rails项目,以删除Active Record的所有残余,包括Pierre Louis提到的所有片段,以及其他类似测试夹具的片段,然后重新创建/重新导入您的模型和测试代码。