运行Rails测试:memory:database

时间:2017-09-08 10:33:01

标签: ruby-on-rails ruby testing

我有一个使用Travis-ci的rails应用程序。为了让这个工作,我要改变

test:
  <<: *default
  database: db/test.sqlite3

test:
    <<: *default
    database: ":memory:"

然而,当我运行rake test test/models/patient_test.rb时,我收到错误但最初有效。错误说

ActiveRecord::PendingMigrationError: 

Migrations are pending. To resolve this issue, run:

        bin/rake db:migrate RAILS_ENV=test

然而,运行此功能并不能解决问题

我的测试是

require 'test_helper'

class PatientTest < ActiveSupport::TestCase
  test "the truth" do
    assert true
  end
end

我的测试帮助文件是

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

1 个答案:

答案 0 :(得分:0)

放下以下一行:

%x|bin/rake db:migrate RAILS_ENV=test|

靠近测试助手顶部的某个地方。原因是每次应用程序启动时都会创建内存数据库,新鲜和空白。它使用普通的sqlite文件,因为它预先填充了迁移。提前运行命令没有多大意义,因为在应用程序启动时重新创建内存数据库