to_yaml 文件未在 rspec(rails 项目)中正确加载
我正在尝试将我的开发 sqlite3 数据库导出到我的测试数据库中。为了做到这一点,我首先在 Rails 控制台中导出模型并将其保存到文件中。
> MyModel.all.to_yaml # this was saved to 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
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好只使用数据库中保存的数据,而不是所有数据都无用所以尝试生成这样的 Yaml:
警告,如果您有大量数据,此技术可能会爆炸您的 RAM。考虑生成带有限制/偏移量的 haml。或使用 will_paginate 中的 pagination_each。
It's better to use only data save on your database not all data useless So try to generate your Yaml like that :
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.