rails中migration数据库后测试不通过的问题

rails项目中由于后期需求变化,需要在products数据库中增加一个字段来满足多国家商品的分类:

rails g migration add_locale_to_products locale:string

完了之后,正常的web app操作均正常,但是末了测试时发生异常:

rake test

打印出一大坨错误 :( ,进入test.sqlite3数据库中没有看到新增的字段.遂使用如下命令强制将数据库更改应用到test数据库:

RAILS_ENV=test rake db:seed

但是还是抛出异常,错误代码依旧:

wisy@wisy-ThinkPad-X61:~/src/ruby_src/rails_test/depot$ RAILS_ENV=test rake db:migrate
rake aborted!
ActiveRecord::UnknownAttributeError: unknown attribute 'locale' for Product.
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:59:in `rescue in _assign_attribute'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:35:in `each'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/core.rb:557:in `init_attributes'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/core.rb:280:in `initialize'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/persistence.rb:33:in `create'
db/seeds.rb:11:in `<top (required)>'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/home/wisy/src/ruby_src/rails_test/depot/config/initializers/make_payment_types_when_test.rb:2:in `<top (required)>'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:652:in `block in load_config_initializer'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/notifications.rb:166:in `instrument'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:651:in `load_config_initializer'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:615:in `each'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:615:in `block in <class:Engine>'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:44:in `each'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:44:in `tsort_each_child'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!'
/home/wisy/src/ruby_src/rails_test/depot/config/environment.rb:5:in `<top (required)>'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:328:in `require'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:328:in `require_environment!'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:443:in `block in run_tasks_blocks'
NoMethodError: undefined method `locale=' for #<Product:0x00000004c7fe30>
/var/lib/gems/2.1.0/gems/activemodel-4.2.0/lib/active_model/attribute_methods.rb:433:in `method_missing'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:54:in `public_send'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:35:in `each'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/core.rb:557:in `init_attributes'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/core.rb:280:in `initialize'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/persistence.rb:33:in `create'
db/seeds.rb:11:in `<top (required)>'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/home/wisy/src/ruby_src/rails_test/depot/config/initializers/make_payment_types_when_test.rb:2:in `<top (required)>'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:652:in `block in load_config_initializer'
/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/notifications.rb:166:in `instrument'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:651:in `load_config_initializer'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:615:in `each'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:615:in `block in <class:Engine>'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:44:in `each'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:44:in `tsort_each_child'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!'
/home/wisy/src/ruby_src/rails_test/depot/config/environment.rb:5:in `<top (required)>'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:328:in `require'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:328:in `require_environment!'
/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:443:in `block in run_tasks_blocks'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

仔细查看错误代码,发现在seeds.rb中出错,其对应的代码是手动添加一个product对象:

Product.delete_all
Product.create(title:"iphone6 plus",
  description:"iphone6 plus目前地球上最好的手机,没有之一!!!",
  image_url:'/images/p6p.jpg',
  price:599.50,
  locale:'cn')

想了一下,这个大概构成了一个死循环:rake db:migrate需要将locale字段加到products中去,但是首先会执行seeds.rb中的Product.create这里locale却还没有呢!
所以现将create代码注释掉,然后执行RAILS_ENV=test rake db:seed,一切正常;接着将该注释的代码重新打开,最后执行rake test,这时原先的异常没有了,取而代之的是几个assert出现F!
首先将products.yml中model新增的locale字段补全:

one:
  title: MyString
  description: MyText
  image_url: /images/one.jpg
  price: 9.99
  locale: 'cn'

然后检查发送F的方法,发现大多都是缺少应用新locale字段的原因,依次添加locale字段即可.

上一篇:RocketMQ/Kafka等消息队列复制的最佳实践


下一篇:设计模式(一): abstract factory抽象工厂模式 -- 创建型模式