使用机械师代替夹具
以下字段
name: string
email: string
children: has_many association to another model
在我的 Rails 3 应用程序中,我有一个 User 模型,其中包含我正在使用的 machinist 2 生成模拟数据,其蓝图看起来像
User.blueprint do
name { 'user{sn}' }
email { '{object.name}@domain.com' }
end
用户的单元测试:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
should have_many( :children )
should validate_uniqueness_of( :email )
should_not allow_value("blah").for(:email)
should_not allow_value("b lah").for(:email)
should allow_value("[email protected]").for(:email)
should allow_value("[email protected]").for(:email)
end
当我生成用户模型时,它创建了一个夹具文件。我的理解是,当我运行 rake 时,Rails 使用该固定文件来生成测试中使用的对象。这不是我想要的。 我希望 Rails 能够像使用夹具文件一样无缝地使用机械师的蓝图。
有办法做到这一点吗?有什么方法可以告诉 Rails 它需要使用蓝图而不是固定装置吗?
In my Rails 3 application, I have a User model with the following fields
name: string
email: string
children: has_many association to another model
I'm using machinist 2 to generate mock data, its blueprint looks like
User.blueprint do
name { 'user{sn}' }
email { '{object.name}@domain.com' }
end
And User's Unit Test:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
should have_many( :children )
should validate_uniqueness_of( :email )
should_not allow_value("blah").for(:email)
should_not allow_value("b lah").for(:email)
should allow_value("[email protected]").for(:email)
should allow_value("[email protected]").for(:email)
end
When I generated the user model, it created a fixture file. My understanding is that when I run rake
, Rails uses that fixture file to generate objects used in the tests. Which is not what I want. I want Rails to use machinist's blueprints just a seamlessly as it uses the fixtures file.
Is there a way to do this? Is there some way to tell rails that it needs to use blueprints instead of fixtures?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其添加到 config/application.rb 中:
您也可以安全地删除旧的固定装置文件夹,除非您显然想保留它们!
Add this to config/application.rb:
You can safely trash the old fixtures folder too, unless you want to keep them obviously!