Machinist测试数据生成工具

Machinist是一款用于Rails应用的测试数据生成工具,它通过定义蓝图来简化测试数据的创建过程。本文介绍了Machinist的安装配置、蓝图定义、Sham类自动生成属性值等功能,并展示了如何处理模型间的关联。
摘要由CSDN通过智能技术生成
作为Rails默认的测试数据生成工具Fixtures很多时候并不能满足要求。所以,基于工厂模式的众多工具纷显。其中,不乏[url=http://hlee.rubyeye.com/admin/blogs/422546]Factory Girl[/url]和Object Daddy这样的工具。然而,本文将介绍另外一个类似的工具Machinist,这个插件同样很有特色。
1. Machinist易于创建数据
2. 处理数据之间的关联
3. 处理和生产重复数据Sham

[b]安装和配置[/b]
作为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 }

[b]
Blueprints[/b]

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")



[b]Sham[/b]

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' }



[b]
处理关联[/b]

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



[b]更多信息[/b]

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值