- 浏览: 110799 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
pobing:
The new way of install rubyhttp ...
ubuntu rvm install ruby & rails -
pobing:
java: sudo apt-get install open ...
ubuntu 11.04 安装及配置rails 开发 环境 -
pobing:
操作完估计:flush privileges;
mysql 添加用户并分配权限 -
chxkyy:
在Ubuntu11中字体存放位置在/usr/share/fon ...
Linux下jodconverter转换pdf中文乱码问题解决¶ -
pobing:
5. install javaupdate-alternati ...
ubuntu 11.04 安装及配置rails 开发 环境
Getting Started
Update Your Gemfile
If you're using Rails, you'll need to change the required version of factory_girl_rails:
gem "factory_girl_rails", "~> 3.0"
If you're not using Rails, you'll just have to change the required version of factory_girl:
gem "factory_girl", "~> 3.0"
Once your Gemfile is updated, you'll want to update your bundle.
Using Without Bundler
If you're not using Bundler, be sure to have the gem installed and call:
require 'factory_girl'
Once required, assuming you have a directory structure of spec/factories or test/factories, all you'll need to do is run
FactoryGirl.find_definitions
If you're using a separate directory structure for your factories, you can change the definition file paths before trying to find definitions:
FactoryGirl.definition_file_paths = %w(custom_factories_directory)
FactoryGirl.find_definitions
If you don't have a separate directory of factories and would like to define them inline, that's possible as well:
require 'factory_girl'
FactoryGirl.define do
factory :user do
name 'John Doe'
date_of_birth { 21.years.ago }
end
end
Defining factories
Each factory has a name and a set of attributes. The name is used to guess the class of the object by default, but it's possible to explicitly specify it:
# This will guess the User class
FactoryGirl.define do
factory :user do
first_name "John"
last_name "Doe"
admin false
end
# This will use the User class (Admin would have been guessed)
factory :admin, class: User do
first_name "Admin"
last_name "User"
admin true
end
end
It is highly recommended that you have one factory for each class that provides the simplest set of attributes necessary to create an instance of that class. If you're creating ActiveRecord objects, that means that you should only provide attributes that are required through validations and that do not have defaults. Other factories can be created through inheritance to cover common scenarios for each class.
Attempting to define multiple factories with the same name will raise an error.
Factories can be defined anywhere, but will be automatically loaded if they are defined in files at the following locations:
test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb
Using factories
factory_girl supports several different build strategies: build, create, attributes_for and stub:
# Returns a User instance that's not saved
user = FactoryGirl.build(:user)
# Returns a saved User instance
user = FactoryGirl.create(:user)
# Returns a hash of attributes that can be used to build a User instance
attrs = FactoryGirl.attributes_for(:user)
# Returns an object with all defined attributes stubbed out
stub = FactoryGirl.build_stubbed(:user)
# Passing a block to any of the methods above will yield the return object
FactoryGirl.create(:user) do |user|
user.posts.create(attributes_for(:post))
end
No matter which strategy is used, it's possible to override the defined attributes by passing a hash:
# Build a User instance and override the first_name property
user = FactoryGirl.build(:user, first_name: "Joe")
user.first_name
# => "Joe"
If repeating "FactoryGirl" is too verbose for you, you can mix the syntax methods in:
# rspec
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
# Test::Unit
class Test::Unit::TestCase
include FactoryGirl::Syntax::Methods
end
# Cucumber
World(FactoryGirl::Syntax::Methods)
This would allow you to write:
describe User, "#full_name" do
subject { create(:user, first_name: "John", last_name: "Doe") }
its(:full_name) { should == "John Doe" }
end
Lazy Attributes
Most factory attributes can be added using static values that are evaluated when the factory is defined, but some attributes (such as associations and other attributes that must be dynamically generated) will need values assigned each time an instance is generated. These "lazy" attributes can be added by passing a block instead of a parameter:
factory :user do
# ...
activation_code { User.generate_activation_code }
date_of_birth { 21.years.ago }
end
In addition to running other methods dynamically, you can use FactoryGirl's syntax methods (like build, create, and generate) within dynamic attributes without having to prefix the call with FactoryGirl.. This allows you to do:
sequence(:random_string) {|n| LoremIpsum.generate }
factory :post do
title { generate(:random_string) } # instead of FactoryGirl.generate(:random_string)
end
factory :comment do
post
body { generate(:random_string) } # instead of FactoryGirl.generate(:random_string)
end
Aliases
Aliases allow you to use named associations more easily.
factory :user, aliases: [:author, :commenter] do
first_name "John"
last_name "Doe"
date_of_birth { 18.years.ago }
end
factory :post do
author
# instead of
# association :author, factory: :user
title "How to read a book effectively"
body "There are five steps involved."
end
factory :comment do
commenter
# instead of
# association :commenter, factory: :user
body "Great article!"
end
Dependent Attributes
Attributes can be based on the values of other attributes using the evaluator that is yielded to lazy attribute blocks:
factory :user do
first_name "Joe"
last_name "Blow"
email { "#{first_name}.#{last_name}@example.com".downcase }
end
FactoryGirl.create(:user, last_name: "Doe").email
# => "joe.doe@example.com"
Transient Attributes
There may be times where your code can be DRYed up by passing in transient attributes to factories.
factory :user do
ignore do
rockstar true
upcased false
end
name { "John Doe#{" - Rockstar" if rockstar}" }
email { "#{name.downcase}@example.com" }
after(:create) do |user, evaluator|
user.name.upcase! if evaluator.upcased
end
end
FactoryGirl.create(:user, upcased: true).name
#=> "JOHN DOE - ROCKSTAR"
Static and dynamic attributes can be ignored. Ignored attributes will be ignored within attributes_for and won't be set on the model, even if the attribute exists or you attempt to override it.
Within FactoryGirl's dynamic attributes, you can access ignored attributes as you would expect. If you need to access the evaluator in a FactoryGirl callback, you'll need to declare a second block argument (for the evaluator) and access ignored attributes from there.
more: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md
Update Your Gemfile
If you're using Rails, you'll need to change the required version of factory_girl_rails:
gem "factory_girl_rails", "~> 3.0"
If you're not using Rails, you'll just have to change the required version of factory_girl:
gem "factory_girl", "~> 3.0"
Once your Gemfile is updated, you'll want to update your bundle.
Using Without Bundler
If you're not using Bundler, be sure to have the gem installed and call:
require 'factory_girl'
Once required, assuming you have a directory structure of spec/factories or test/factories, all you'll need to do is run
FactoryGirl.find_definitions
If you're using a separate directory structure for your factories, you can change the definition file paths before trying to find definitions:
FactoryGirl.definition_file_paths = %w(custom_factories_directory)
FactoryGirl.find_definitions
If you don't have a separate directory of factories and would like to define them inline, that's possible as well:
require 'factory_girl'
FactoryGirl.define do
factory :user do
name 'John Doe'
date_of_birth { 21.years.ago }
end
end
Defining factories
Each factory has a name and a set of attributes. The name is used to guess the class of the object by default, but it's possible to explicitly specify it:
# This will guess the User class
FactoryGirl.define do
factory :user do
first_name "John"
last_name "Doe"
admin false
end
# This will use the User class (Admin would have been guessed)
factory :admin, class: User do
first_name "Admin"
last_name "User"
admin true
end
end
It is highly recommended that you have one factory for each class that provides the simplest set of attributes necessary to create an instance of that class. If you're creating ActiveRecord objects, that means that you should only provide attributes that are required through validations and that do not have defaults. Other factories can be created through inheritance to cover common scenarios for each class.
Attempting to define multiple factories with the same name will raise an error.
Factories can be defined anywhere, but will be automatically loaded if they are defined in files at the following locations:
test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb
Using factories
factory_girl supports several different build strategies: build, create, attributes_for and stub:
# Returns a User instance that's not saved
user = FactoryGirl.build(:user)
# Returns a saved User instance
user = FactoryGirl.create(:user)
# Returns a hash of attributes that can be used to build a User instance
attrs = FactoryGirl.attributes_for(:user)
# Returns an object with all defined attributes stubbed out
stub = FactoryGirl.build_stubbed(:user)
# Passing a block to any of the methods above will yield the return object
FactoryGirl.create(:user) do |user|
user.posts.create(attributes_for(:post))
end
No matter which strategy is used, it's possible to override the defined attributes by passing a hash:
# Build a User instance and override the first_name property
user = FactoryGirl.build(:user, first_name: "Joe")
user.first_name
# => "Joe"
If repeating "FactoryGirl" is too verbose for you, you can mix the syntax methods in:
# rspec
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
# Test::Unit
class Test::Unit::TestCase
include FactoryGirl::Syntax::Methods
end
# Cucumber
World(FactoryGirl::Syntax::Methods)
This would allow you to write:
describe User, "#full_name" do
subject { create(:user, first_name: "John", last_name: "Doe") }
its(:full_name) { should == "John Doe" }
end
Lazy Attributes
Most factory attributes can be added using static values that are evaluated when the factory is defined, but some attributes (such as associations and other attributes that must be dynamically generated) will need values assigned each time an instance is generated. These "lazy" attributes can be added by passing a block instead of a parameter:
factory :user do
# ...
activation_code { User.generate_activation_code }
date_of_birth { 21.years.ago }
end
In addition to running other methods dynamically, you can use FactoryGirl's syntax methods (like build, create, and generate) within dynamic attributes without having to prefix the call with FactoryGirl.. This allows you to do:
sequence(:random_string) {|n| LoremIpsum.generate }
factory :post do
title { generate(:random_string) } # instead of FactoryGirl.generate(:random_string)
end
factory :comment do
post
body { generate(:random_string) } # instead of FactoryGirl.generate(:random_string)
end
Aliases
Aliases allow you to use named associations more easily.
factory :user, aliases: [:author, :commenter] do
first_name "John"
last_name "Doe"
date_of_birth { 18.years.ago }
end
factory :post do
author
# instead of
# association :author, factory: :user
title "How to read a book effectively"
body "There are five steps involved."
end
factory :comment do
commenter
# instead of
# association :commenter, factory: :user
body "Great article!"
end
Dependent Attributes
Attributes can be based on the values of other attributes using the evaluator that is yielded to lazy attribute blocks:
factory :user do
first_name "Joe"
last_name "Blow"
email { "#{first_name}.#{last_name}@example.com".downcase }
end
FactoryGirl.create(:user, last_name: "Doe").email
# => "joe.doe@example.com"
Transient Attributes
There may be times where your code can be DRYed up by passing in transient attributes to factories.
factory :user do
ignore do
rockstar true
upcased false
end
name { "John Doe#{" - Rockstar" if rockstar}" }
email { "#{name.downcase}@example.com" }
after(:create) do |user, evaluator|
user.name.upcase! if evaluator.upcased
end
end
FactoryGirl.create(:user, upcased: true).name
#=> "JOHN DOE - ROCKSTAR"
Static and dynamic attributes can be ignored. Ignored attributes will be ignored within attributes_for and won't be set on the model, even if the attribute exists or you attempt to override it.
Within FactoryGirl's dynamic attributes, you can access ignored attributes as you would expect. If you need to access the evaluator in a FactoryGirl callback, you'll need to declare a second block argument (for the evaluator) and access ignored attributes from there.
more: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md
发表评论
-
google-charts-on-rails
2012-07-06 19:04 912Google Chart API 参考 中文版 http:// ... -
ruby 判断客户端浏览器类型代码
2012-05-31 18:41 1874def users_browser user_age ... -
Rails 开发环境日志过大时自动清理
2012-05-24 16:52 829Rails 开发环境日志过大时自动清理 新建文件:confi ... -
ubuntu rvm install ruby & rails
2012-05-12 16:15 1528source : http://ryanbigg.com/20 ... -
rails gsub,regexp ,match 的应用
2012-04-26 15:14 11361.将 电话号码的中的 '/' ... -
Rails常用命令整理
2012-02-02 11:47 796------ GEM: gem install rails ... -
如何从零开始学会 Ruby on Rails?
2012-01-29 16:25 727http://huacnlee.com/blog/how-to ... -
如何快速正确的安装 Ruby, Rails 运行环境
2012-01-29 16:24 819source:http://ruby-china.org/wi ... -
rails 生产(production)模式下 上传图片不显示问题解决
2012-01-05 13:13 1027如果是 rails s -e production 启动服务的 ... -
unicorn.rb 配置
2012-01-04 10:02 1392# Sample verbose configuration ... -
Installing ruby-debug19 with ruby 1.9.3 on rvm
2012-01-02 23:11 746http://rails.vandenabeele.com/b ... -
部署rails 项目 nginx(apache)+passenger
2011-12-16 16:36 1885一 、apache+passenger 1. 安 ... -
remove rvm
2011-12-15 17:38 1006To remove RVM, first you need t ... -
ubuntu 11.04 安装及配置rails 开发 环境
2011-12-15 17:30 12871.install openssh-server http:/ ... -
centos6 安装ruby on rails环境并部署(nginx+unicorn)
2011-12-14 16:57 43801.第一步 su root *** 安装 ... -
BDD rspec with rails3 project
2011-11-30 18:30 1586参考 源自Ruby迷>>原文链接地址:RSp ... -
rails在windows下安装遇到的问题
2011-10-16 11:14 887原文地址:http://my.oschina.net/u/1 ...
相关推荐
factory_girl-seeds, 为factory_girl预加载可以重用记录 FactoryGirl种子不喜欢 factory_girl,因为它很慢? 你知道通过factory_girl创建的记录可以以占用最多 50%个规格的运行时间? 更多 !这个小 gem 可以通过在...
factory_girl_extensions 是一组简单的语法扩展,我通常喜欢在使用 factory_girl 时在项目中使用它。 安装 gem install factory_girl_extensions FactoryGirl 扩展 2.0 从 2.0 版开始, factory_girl_extensions ...
factory_girl_api factory_girl_api gem是一个Rails引擎,它与集成在一起,以添加用于处理工厂的API路由,这些路由可用于前端测试中的上下文设置。 它与,后者提供了Angular服务,该服务将在测试期间向您的服务器...
工厂女童协会回电 工厂女孩的自定义策略, before关联属性的回调before和after添加。... # after require 'factory_girl' # Replace default factory girl strategies require Rails.root.join('vendor/factory_
从rails的factory_girl移植到nodejs的端口功能 安装 Node.js: npm install 'factory_girl' 用法 定义工厂 var FactoryGirl = require ( 'factory_girl' ) ; // for nodejs FactoryGirl . define ( 'user' , ...
knoll_light_factory_2_win下载
Factory_Test_Mode Factory_Test_Mode
pytest-factoryboy, factory_boy集成 pytest runner factory_boy与 pytest的集成 通过pytest注入,可以很容易地将 factory 方法与注入( pytest fixture )的测试设置结合起来。安装 pytest-factoryboy
VB多线程各种例子Thread Factory 4(中文帮助文档)
从factory_girl过渡? 查看 。 文献资料 您应该在上找到有关版本的文档。 有关定义和使用工厂的信息,请参见 。 我们还提供,可在Upcase上免费获得。 安装 将以下行添加到Gemfile中: gem 'factory_bot' 并从您...
从factory_girl_rails过渡? 查看 。 滑轨 factory_bot_rails提供的Rails集成 。 中列出了受支持的Rails版本。 中列出了受支持的Ruby版本。 下载 GitHub: : 宝石: $ gem install factory_bot_rails 配置 添加...
Datafactory_使用指导书(V5.6.0)
Factory_Physics_2e,工厂物理学第2版的讲义,一系列PPT
DataFactory_软件_key_操作指南.rar
Nokia_Multimedia_Factory_2_0
Setup_Factory_7 教程,分进阶篇,问答篇
国外著名工控组态软件中用的ActiveX控件库。非常棒!包含了泵、阀门、电机、管道、容器等所有的对象。开发工具: Visual Basic,支持rsview32、ifix.
例如,您使用FFaker::Job.title生成所有name或title的值,并使用FFaker::Lorem.paragraph生成description或content的值。 然后,您只需要将这些方法复制并粘贴到服务器工厂,甚至服务器项目工厂中的服务器列中即可...
Hibernate_session_factory_配置
Setup_Factory_7.0.1教程.pdf