【问题标题】:Rails functional tests fail for MongoDB with ActiveRecord/SQLite3 errorMongoDB 的 Rails 功能测试失败,出现 ActiveRecord/SQLite3 错误
【发布时间】:2013-06-30 17:11:25
【问题描述】:

我将 Mongoid 与 Rails 一起使用,但所有已生成的功能测试都失败并出现类似以下错误:

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,因为我在删除它时遇到了问题,并且仍然不确定如何从应用程序中完全删除它,因为我没有将它用于任何事情)

【问题讨论】:

    标签: ruby-on-rails sqlite mongodb activerecord mongoid


    【解决方案1】:

    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吗?

    【讨论】:

      【解决方案2】:

      如果你输入

      rails --help
      

      然后会列出跳过 Active Record 的选项

      -O, [--skip-active-record]     # Skip Active Record files
      

      我建议您使用此选项创建一个新的 Rails 项目,以删除所有 Active Record 痕迹,包括 Pierre-Louis 提到的所有部分,以及其他测试装置等,然后重新创建/重新导入您的模型和测试代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-29
        • 1970-01-01
        • 2016-03-05
        • 2016-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多