作为Rails默认的测试数据生成工具Fixtures很多时候并不能满足要求。所以,基于工厂模式的众多工具纷显。其中,不乏
Factory Girl和Object Daddy这样的工具。然而,本文将介绍另外一个类似的工具Machinist,这个插件同样很有特色。
1. Machinist易于创建数据
2. 处理数据之间的关联
3. 处理和生产重复数据Sham
安装和配置
作为Rails的插件,如下安装:
./script/plugin install git://github.com/notahat/machinist.git
在spec目录下创建blueprints.rb
require File.expand_path(File.dirname(__FILE__) + "/blueprints")
当然,如果你要需要Cucumber的功能,要添加require在features/steps/env.rb
require File.join(RAILS_ROOT, 'spec', 'blueprint')
如果使用Sham的功能要修改spec_helper.rb中的
config.before(:each) { Sham.reset }
Blueprints
Machinist defines blueprints for your models. According to Pete:
A blueprint describes how to build a generic object for an ActiveRecord model. The idea is that you let the blueprint take care of constructing all the objects and attributes that you don’t care about in your test, leaving you to focus on the just the things that you’re testing.
Business.blueprint do
name { "My Company" }
address { "No.01, Down Street" }
web { "http://www.example.com" }
email { "info@example.com" }
end
Machinist will create objects by calling the save! method on the ActiveRecord model. Hence, it will throw exceptions if the validations failed. To create a new object from a blueprint, call make method on model.
business = Business.make
When testing a particular field, you could override its value by passing a parameter to make.
invalid_business = Business.make(:email => "bad@email")
Sham
Without setting values manually for attributes, you could use Machinist’s Sham class to auto-generate attribute values. Use Faker gem with Sham to easily generate the dummy values.
Here is how to define Sham methods.
Sham.name { Faker::Name.name }
Sham.business_name { Faker::Company.name }
Sham.email { Faker::Internet.email }
Sham.address { Faker::Address.street_address }
Sham.web { Faker::Internet.domain_name }
We could modify the above example of Business blueprint to use the Sham defined.
Business.blueprint do
name { Sham.business_name }
address { Sham.address }
web { Sham.web }
email { Sham.email }
end
You could generate sequences with Sham by offering a block parameter.
Sham.invoice_no {|index| "20080101-#{index}" }
Sham ensures each object created will contain unique attribute values. If you want to have duplicate values, you could pass the :unique option. For example:
Sham.coin_toss(:unique => false) { rand(2) == 0 ? 'heads' : 'tails' }
处理关联
With Machinist creating associated objects is simple. You just have to define the associated object as an attribute.
User.blueprint do
name { Sham.name }
email { Sham.email }
business
end
Further you could customize the associating object by passing attributes to make call.
User.blueprint do
name { Sham.name }
email { Sham.email }
business { Business.make(:name => "#{name} Shop") }
end
更多信息
I hope you got a basic understanding of Machinist and how it could be helpful in your tests. For more information and latest developments of Machinist, please visit its Github repo. Also, there is another good post on Machinist (with comparisons to FactoryGirl) by Tim Lucas.
分享到:
相关推荐
赠送jar包:machinist_2.12-0.6.1.jar; 赠送原API文档:machinist_2.12-0.6.1-javadoc.jar; 赠送源代码:machinist_2.12-0.6.1-sources.jar; 赠送Maven依赖信息文件:machinist_2.12-0.6.1.pom; 包含翻译后的API...
讲故事的脚手架和静态站点生成器。 快速开始 有关建议的本地安装,请参阅。 默认情况下, machinist init将自动设置生成项目的月份和年份。 安装Node.js (需要v8.x) npm install -g @nbcnews/machinist ...
赠送jar包:machinist_2.12-0.6.1.jar; 赠送原API文档:machinist_2.12-0.6.1-javadoc.jar; 赠送源代码:machinist_2.12-0.6.1-sources.jar; 赠送Maven依赖信息文件:machinist_2.12-0.6.1.pom; 包含翻译后的API...
安装$ pip install machinist 机械师的自动结构化日志记录取决于 。 日志记录被声明为Machinist Extra,因此您可以自动安装此依赖项: $ pip install machinist[logging]定义输入,输出和状态输入,输出和状态都为...
通过添加frontmatter model: file_name.json将结构化数据添加到特定的Markdown文件model: file_name.json ,而file_name.json位于./src/models/ 要求 如何设定 1.克隆 git clone ...
安装您可以安装machinist将它添加到您的依赖项列表mix.exs : def deps do [ { :machinist , " ~> 0.1.0 " } ]end用法一个很好的例子是我们如何实现门的行为。 与machinist将是这样的: defmodule Door do defstruct...
一组工具/计算器,可帮助CNC机械师快速处理与机床技术相关的数学。 准备就绪时将包含更多的计算器,最终产品可能是一个单独的组合应用程序。 当前可用的是一个递归的“铣削速率”计算器,这意味着它在您处理数据时会...
* 1998 年,达索接管法国马特拉( MATRA)下属的 CAD/CAM 业务,包括 Euclid/ styler,Machinist,Cisigraph/ Strim100 和 StrimFlow 等。 * 1999 年,达索收购 Smartsolutions 的 SmarTeam 业务。 * 1999 年,达索向...
组件的子类型则涉及设计、互换、校验等多个方面,而制造子类型涵盖NC组件、Expert Machinist等,这些都是为了实现高效的制造流程。 在模具设计中,Pro/ENGINEER提供了丰富的功能。例如,模具设计菜单管理器允许用户...
MRI (Machinist Robot Interface) 是FRC中的一种软件框架,它允许开发者编写控制机器人行为的代码。2015年的MRI Kratos更新,不仅提升了系统的稳定性和性能,还引入了更先进的功能,以适应当年的竞赛规则。这一更新...