Rails和MongoDB测试:ActiveRecord :: StatementInvalid:SQLite3 :: SQLException:没有这样的表:用户

时间:2017-03-25 06:01:49

标签: ruby-on-rails ruby mongodb tdd mongoid

我制作了一个用户模型,我正在尝试测试它。

我是使用MongoDB的新手。以前我使用过SQLite。

我的测试文件包含以下内容:

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = users(:john)
  end

  test "should be valid" do
    assert @user.valid?
  end

end

夹具:

john:
  email: john@example
  first_name: John
  last_name: Doe
  date_of_birth: Date.new(1980,5,6)
  gender: male
  occupation: business analyst

当我运行测试时,我收到以下错误:

ERROR["test_should_be_valid", UserTest, 0.02658726099616615]
 test_should_be_valid#UserTest (0.03s)
ActiveRecord::StatementInvalid:         ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: users: DELETE FROM "users"

我认为测试文件在使用Mongo时会尝试使用ActiveRecord。我不确定如何解决这个问题。这是我的user.rb模型文件:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  field :email, type: String
  field :first_name, type: String
  field :last_name, type: String
  field :date_of_birth, type: Date
  field :gender, type: String
  field :occupation, type: String

  embeds_one :location, class_name: 'Place', as: :locatable

  #backwards-compatible readers
  def name
    "#{first_name} #{last_name}"
  end

  def age(dob=self.date_of_birth)
    now = Time.now.utc.to_date
    now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
  end

end

编辑:

在test_helper.rb的顶部添加了以下内容:

require 'mongoid'
Mongoid.load!("./config/mongoid.yml", "test")

仍然遇到同样的错误。

0 个答案:

没有答案