to_yaml 文件未在 rspec(rails 项目)中正确加载 [英] to_yaml file not loading properly in rspec (rails project)

查看:34
本文介绍了to_yaml 文件未在 rspec(rails 项目)中正确加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的开发 sqlite3 数据库导出到我的测试数据库中.为了做到这一点,我首先在 rails 控制台中导出了我的模型并将其保存到一个文件中.

<代码>>MyModel.all.to_yaml # 保存到 mymodels.yml

现在,当我运行 rspec 时,它在尝试解析 mymodels.yml 时失败.我得到的错误是:

<块引用>

失败/错误:无法从回溯中找到匹配的行解析/Users/MakeM/MyProject1/spec/fixtures/mymodels.yml 时发生 YAML 错误.

请注意,YAML 必须是始终使用空格缩进.不允许使用标签.请有一个看看 http://www.yaml.org/faq.html确切的错误是:NoMethodError: nil:NilClass 的未定义方法`keys

知道为什么我会收到这个错误吗?我应该提到的一件事是 to_yaml 输出的 yaml 对我来说看起来有点奇怪.这是其中的一部分:

---- !ruby/object:MyModel属性:编号:133书:第一本书章节:50created_at: 2010-10-06 05:03:15.709931更新时间:2010-10-06 05:03:15.709931缩写:FB属性缓存:{}changed_attributes:{}毁坏:假标记_for_destruction: 假新记录:假以前_更改:{}只读:假

解决方案

最好只使用保存在数据库中的数据,而不是所有数据都没用 所以尝试像这样生成 Yaml :

MyModel.all.map(&:attributes).to_yaml

警告,如果您有大量数据,此技术可能会导致 RAM 爆炸.考虑用限制/偏移量生成你的haml.或者使用 will_pagination 中的 paginated_each.

I am trying to export my development sqlite3 database into my test database. In order to do this I first exported my model in rails console and saved it to a file.

> MyModel.all.to_yaml   # this was saved to mymodels.yml

Now when I run rspec it fails while trying to parse mymodels.yml. The error I get is:

Failure/Error: Unable to find matching line from backtrace
 a YAML error occurred parsing /Users/MakeM/MyProject1/spec/fixtures/mymodels.yml.

Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html The exact error was: NoMethodError: undefined method `keys' for nil:NilClass

Any idea why I am getting this error? One thing I should mention is that the yaml that is output by to_yaml looks a bit strange to me. Here's part of it:

--- 
- !ruby/object:MyModel 
  attributes: 
    id: 133
    book: FirstBook
    chapters: 50
    created_at: 2010-10-06 05:03:15.709931
    updated_at: 2010-10-06 05:03:15.709931
    abbr: FB
  attributes_cache: {}

  changed_attributes: {}

  destroyed: false
  marked_for_destruction: false
  new_record: false
  previously_changed: {}

  readonly: false

解决方案

It's better to use only data save on your database not all data useless So try to generate your Yaml like that :

MyModel.all.map(&:attributes).to_yaml

Warning, this technics can explode your RAM if you have a lot of data. Think to generate your haml with limit / offset. or with paginated_each from will_paginate.

这篇关于to_yaml 文件未在 rspec(rails 项目)中正确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆