如何在运行规范时更改我的数据库架构?
我想测试一个模型混合,所以我想创建一个假的 ActiveRecord 模型来包含它,然后测试该模型的行为。
我从另一个上下文获得的规范要求假模型的数据库表存在(我知道,我不应该依赖于此,但现在就是这样)。因此,我需要在规范运行时为假模型 (TestModel
) 创建一个表。
我该怎么做?
I want to test a model mixin, so I want to create a fake activerecord model to include it in, and then test the behavior of that model.
The specs I have from another context require the db table of the fake model to exist (I know, I shouldn't depend on this, but that's how they are for now). So I need to create a table for the fake model (TestModel
) while the spec is running.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,将伪造的模型视为外部服务并模拟其他模型的容器。 一样
像正常运行测试
In this case, treat the faked model as you would an external service and mock out the other model's container. Something like
Run tests as normal
这可能看起来有点矫枉过正,
但在我看来,如果一个库需要进行抽象测试
也许值得将其放入 gem 或插件中。
一般来说,在我的应用程序中,我测试 mixins 的结果,而不是 mixins 本身。
并将 mixin 测试保留在相关插件或 gem 中。
如果包含重要的业务逻辑(或者您的公司不会让您开源它),请将其保留为插件。
如果它普遍可用,那么将其分解为宝石。
这是我提取的宝石的示例
http://bit.ly/hJ4QC2
几乎只需使用
rails g 插件
模板并添加如下内容,您应该为自己进行一个插件测试
This might seem like overkill,
but in my view, if a library needs to be tested in abstraction
it may be worth pulling it out into a gem or plugin.
Generally in my app I test the consequence of mixins, rather than the mixins themselves.
And keep the mixin tests inside the relevant plugin or gem.
If the contains important business logic (or your company won't let you open source it) keep it as a plugin.
If its generally usable, then break it out as a gem.
Here's an example of a gem I abstracted
http://bit.ly/hJ4QC2
Pretty much just use the
rails g plugin
template and add something like the followingand you should have yourself a plugin test