【问题标题】:Rspec Load The Past(Wrong) ModelRspec 加载过去(错误)模型
【发布时间】:2015-09-29 03:06:32
【问题描述】:

我有一个 Rails 迁移文件

class CreateUserData < ActiveRecord::Migration
  def change
    create_table :user_data do |t|
      t.belongs_to :user, index: true
      t.string :country
      t.string :city
      t.string :state
      t.string :language
      t.string :device_advertising_id
      t.string :client_type
      t.string :data_type

      t.timestamps
    end
  end
end

我玩了几次。然后,我把它改成了。

class CreateUserData < ActiveRecord::Migration
  def change
    create_table :user_data do |t|
      t.belongs_to :user, index: true
      t.string :country
      t.string :city
      t.string :sublocality # added
      t.string :zip_code # added
      t.string :language
      t.string :device_advertising_id
      t.string :client_type
      t.string :data_type

      t.timestamps
    end
  end
end

这是模型文件。

# a class to store user data based on initial and latest
class UserData < ActiveRecord::Base
  belongs_to :user, class_name: 'Spree::User'
  validates :device_advertising_id, presence: true

  enum data_type: { initial: 'initial', latest: 'latest' }

  scope :no_initial, -> { where(device_advertising_id: device_advertising_id).where(data_type: 'initial') }

  def first_update(country='', city='', sublocality='', zip_code='', language='', client_type='')
    self.country ||= country
    self.city ||= city
    self.sublocality ||= sublocality
    self.zip_code ||= zip_code
    self.language ||= language
    self.client_type ||= client_type
  end
end

当我在 Rails 控制台上检查 UserData 模型时

UserData(id: integer, user_id: integer, country: string, city: string, 子地区:字符串,邮编:字符串,语言:字符串, device_advertising_id:字符串,client_type:字符串,data_type:字符串, created_at:日期时间,updated_at:日期时间)

但是,当我运行 rspec.我的工厂失败了。 undefined method sublocality= for #&lt;UserData:0x007fd144230830&gt;

当我运行 byebug 并检查课程时。

UserData(id: integer, user_id: integer, country: string, city: string, 状态:字符串,语言:字符串,device_advertising_id:字符串, client_type: string, created_at: datetime, updated_at: datetime)

知道为什么 rspec 总是加载该类的过去版本吗?我已经迁移到最新的迁移并检查了数据库表。

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    对不起,我忘记了 Rails 测试数据库与开发数据库不同。我只需要跑。

    rake db:rollback RAILS_ENV=test
    rake db:migrate RAILS_ENV=test
    

    让它工作。

    【讨论】:

    • 会的,但是我需要等两天。 :)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多