在 Rails 3.2 中,每个模型是否可以有多个夹具文件? [英] Is it possible to have multiple fixture files per model in rails 3.2?

查看:36
本文介绍了在 Rails 3.2 中,每个模型是否可以有多个夹具文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails 3.2 中,是否可以为一个给定的ActiveRecord 对象拥有多个fixture 文件?

In Rails 3.2, is it possible to have multiple fixture files for a given ActiveRecord object?

客户要求将测试数据写入固定装置,但也希望它们易于管理.我想通过引入第二组来将固定装置分开一点,原始装置将包含/要求/渲染任何内容.

The client is requiring the test data be written in fixtures but also wants them to be manageable. I'd like to split up the fixtures a bit by introducing a 2nd set that the originals would include/require/render whatever.

我无法通过 Google 找到任何有关如何操作的信息,而且固定装置不是我的菜.提前致谢.

I haven't been able to find anything via Google on how to do it and fixtures are not my cup of tea. Thanks in advance.

推荐答案

在我看来,您想要做的是从另一个 YAML 文件中包含一个 YAML 文件.这是一个涵盖如何做到这一点的问题:如何在 YAML 文件中包含 YAML 文件?

Seems to me like what you want to do is include a YAML file from another YAML file. Here's a question which covers how to do that: How to include a YAML file inside a YAML file?

由于fixtures已经有ERB,所以应该很简单:

Since fixtures already have ERB, it should be as simple as:

<%= IO.read(Rails.root.join "test/other_fixtures/fixture_to_load.yml") %>

只需确保夹具位于主夹具目录之外,否则加载夹具的机制也会尝试将它们映射到模型.

Just make sure that the fixtures are outside of the primary fixture directory or the mechanism for loading fixtures will also attempt to map them to a model.

如果需要在fixture内部使用ERB,将其包裹在ERB.new中,如:

If you need ERB inside of the fixture, wrap it in ERB.new, such as:

<%= ERB.new(IO.read(Rails.root.join "test/other_fixtures/fixture_to_load.yml")).result %>

这篇关于在 Rails 3.2 中,每个模型是否可以有多个夹具文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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