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字段即可.

相关文章
|
3月前
|
JavaScript 前端开发 数据库
测试开发之路--Flask 之旅 (三):数据库
本文介绍了在 Flask 应用中实现权限管理的过程,包括使用 Flask-SQLAlchemy、Flask-MySQLdb、Flask-Security 和 Flask-Login 等扩展模块进行数据库配置与用户权限设置。首先创建数据库并定义用户、环境和角色模型,接着通过 Flask-Security 初始化用户和角色,并展示了如何便捷地管理权限。后续将深入探讨权限控制的具体应用。
75 4
测试开发之路--Flask 之旅 (三):数据库
|
2月前
|
NoSQL 关系型数据库 MySQL
AWS Database Migration Service 助力数据库搬迁
AWS Database Migration Service 助力数据库搬迁
|
3月前
|
关系型数据库 MySQL 数据库
6-2|测试连接数据库的命令
6-2|测试连接数据库的命令
|
3月前
|
关系型数据库 MySQL 测试技术
《性能测试》读书笔记_数据库优化
《性能测试》读书笔记_数据库优化
33 7
|
7月前
|
JavaScript Java 测试技术
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
109 0
|
4月前
|
安全 测试技术 网络安全
深入理解数据库黑盒测试
【8月更文挑战第31天】
51 0
|
5月前
|
监控 Oracle 关系型数据库
关系型数据库Oracle恢复测试
【7月更文挑战第20天】
83 7
|
5月前
|
SQL 安全 数据库
Ruby on Rails 数据库迁移操作深度解析
【7月更文挑战第19天】Rails 的数据库迁移功能是一个强大的工具,它帮助开发者以版本控制的方式管理数据库结构的变更。通过遵循最佳实践,并合理利用 Rails 提供的迁移命令和方法,我们可以更加高效、安全地管理数据库结构,确保应用的稳定性和可扩展性。
|
5月前
|
关系型数据库 MySQL 测试技术
数据库升级是一个涉及数据备份、新版本安装、数据迁移和测试等关键环节的复杂过程
【7月更文挑战第21天】数据库升级是一个涉及数据备份、新版本安装、数据迁移和测试等关键环节的复杂过程
124 1
|
5月前
|
中间件 Java 测试技术
单元测试问题之编写单元测试时运行环境、数据库、中间件问题如何解决
单元测试问题之编写单元测试时运行环境、数据库、中间件问题如何解决