Edwin Lunando
Edwin Lunando

Reputation: 2816

Rspec Load The Past(Wrong) Model

I have a Rails migration file

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

I play with it a couple of times. Then, I changed it into.

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

This is the model file.

# 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

When I inspect the UserData model on rails console

UserData(id: integer, user_id: integer, country: string, city: string, sublocality: string, zip_code: string, language: string, device_advertising_id: string, client_type: string, data_type: string, created_at: datetime, updated_at: datetime)

But, when I ran rspec. My Factory fails. undefined method sublocality= for #<UserData:0x007fd144230830>

When I ran byebug and inspect the class.

UserData(id: integer, user_id: integer, country: string, city: string, state: string, language: string, device_advertising_id: string, client_type: string, created_at: datetime, updated_at: datetime)

Any idea why rspec always loading the past version of the class? I already migrated to the newest migration and checked the database table.

Upvotes: 1

Views: 110

Answers (1)

Edwin Lunando
Edwin Lunando

Reputation: 2816

I'm sorry I forgot that Rails test database is different with the development one. I only need to run.

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

to make it work.

Upvotes: 2

Related Questions