文章是从个人博客转过来的,  可以直接访问 iwangzheng.com

https://github.com/thoughtbot/factory_girl

https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

http://ruby.taobao.org/

今天项目结束,打算搞一下接手这个项目之前的单元测试,因为接受的时候很多测试是跑不通的,于是重新学习了一下 factory_girl

gem install factory_girl (半天没响应)

由于国内网络原因(你懂的),导致 rubygems.org 存放在 Amazon S3 上面的资源文件间歇性连接失败。所以你会与遇到 gem install rack 或 bundle install 的时候半天没有响应,具体可以用 gem install rails -V 来查看执行过程。现在来更换一下gem source

$ gem sources --remove https://rubygems.org/
$ gem sources -a http://ruby.taobao.org/
$ gem sources -l
*** CURRENT SOURCES *** http://ruby.taobao.org
# 请确保只有 ruby.taobao.org

Configure your test suite

# 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) # Spinach
class Spinach::FeatureSteps
include FactoryGirl::Syntax::Methods
end # MiniTest
class MiniTest::Unit::TestCase
include FactoryGirl::Syntax::Methods
end # MiniTest::Spec
class MiniTest::Spec
include FactoryGirl::Syntax::Methods
end # minitest-rails
class MiniTest::Rails::ActiveSupport::TestCase
include FactoryGirl::Syntax::Methods
end 由于我们用的是rspec,所以选第一种,所在spec/spec_helper.rb里配置第18行那句

1 # -*- encoding : utf-8 -*-
2 # This file is copied to spec/ when you run 'rails generate rspec:install'
3 ENV["RAILS_ENV"] ||= 'test'
4 require File.expand_path("../../config/environment", __FILE__)
5 require 'rspec/rails'
6 require 'rspec/autorun'
7 require "email_spec"
8
9 # Requires supporting ruby files with custom matchers and macros, etc,
10 # in spec/support/ and its subdirectories.
11 Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
12 Dir[Rails.root.join("lib/mock*.rb")].each {|f| require f}
13
14 RSpec.configure do |config|
15 config.fixture_path = "#{::Rails.root}/spec/fixtures"
16 config.use_transactional_fixtures = true
17 config.infer_base_class_for_anonymous_controllers = false
18 config.include FactoryGirl::Syntax::Methods
19 config.global_fixtures = :all
20 config.include(EmailSpec::Helpers)
21 config.include(EmailSpec::Matchers)
22 end

Defining factories

# 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

一般在factories的文件夹,以modle名字来命名factory,这样更清晰

来看看我写的这段

FactoryGirl.define do
    factory :cms_tv_version do
       title "2.0.0"
       state 1
    end
end

 

gem install factory_girl的更多相关文章

  1. mac gem install nokogiri error

    Gem::Ext::BuildError: ERROR: Failed to build gem native extension. /Users/angela/.rbenv/versions/1.9 ...

  2. OSX 10.11 cocoapods安装命令: sudo gem install -n /usr/local/bin cocoapods

    10.11 cocoapods安装命令: sudo gem install -n /usr/local/bin cocoapods

  3. gem install 出现Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org

    在安装了rvm来管理多版本的ruby之后,想在不同环境下安装一些gems,结果gem install puma 之后,发现一次又一次失败. gem install 出现Errno::ECONNRESE ...

  4. sudo gem install cocoapods 没反应问题

    1. 尝试更新 sudo gem update --system 2. 查看安装详细 sudo gem install cocoapods -V 3.详细使用有个链接 http://blog.csdn ...

  5. ruby on rails gem install pg时无法安装

    gem install pg -v '0.18.2' Building native extensions. This could take a while... ERROR: Error insta ...

  6. sudo gem install cocoapods

    在使用IOS_BaiduSDK的时候,需要用到cocoapods,所以就需要按照步骤继续着.但是在过程中会遇到一些问题: 1. sudo gem install cocoapods 运行这个报错 Ru ...

  7. 在openSUSE13.2上gem install rails -v 4.1成功,但是之后不存在rails命令解决

    解决方案为,不要用sudo gem install就好了,卧槽

  8. 本地安装gem install --local redis-stat-0.4.13.gem

    因为主机环境不能联外网,悲哀,所以只能想办法下载包,上传到主机来安装 环境:el6.x86_64 1. gem 安装[http://centos.ustc.edu.cn/centos/6/os/x86 ...

  9. gem install走代理,速度刚刚的

    有个树莓pi,安装了shadowsocks 和 cow ,做代理,走ipv6,学校不收ipv6流量钱.速度也不错,快的下载可达10M/s. gem install xx遇到墙了. nano ~/.ge ...

随机推荐

  1. 从零开始写redis客户端(deerlet-redis-client)之路——第一个纠结很久的问题,restore引发的血案

    引言 正如之前的一篇博文,LZ最近正在从零开始写一个redis的客户端,主要目的是为了更加深入的了解redis,当然了,LZ也希望deerlet客户端有一天能有一席之地.在写的过程当中,LZ遇到了一个 ...

  2. document.cookie打不出来cookies

    比如session这种设置,都是设置了HttpOnly 导致document.cookie看不到,这和xss  跨站脚本攻击(Cross Site Scripting)

  3. get与post

    1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...

  4. OWIN-WebAPI-Windows Service

    tks: https://github.com/danesparza/OWIN-WebAPI-Service add 2015 0717:http://kb.cnblogs.com/page/5092 ...

  5. Git学习笔记——一个NB的分布式版本控制系统

    1. 命令: git init           创建新仓库 (在一个空文件下然后执行命令) git clone  + 路径      检出仓库,从本地或从服务器上 git status     查 ...

  6. MVC学习Day01

    ~~~~ =============================================================================================== ...

  7. 【CodeForces 599A】D - 特别水的题4- Patrick and Shopping

    Description  meter long road between his house and the first shop and a d2 meter long road between h ...

  8. Cocos2d-X3.0 刨根问底(二)----- 从HelloWorld开始

    小鱼习惯直接从代码实例来学习一套成型的引擎库. 运行cpp-empty-test 一个典型的HelloWorld程序翻看代码结构 看到了 main.h与main.cpp文件就从这里开始 #ifndef ...

  9. OI历程日常

    之前的一直没来的及记录,表示从今往后连载 10.29 蒟蒻正在紧张的备战NOIP 整改了一下faebdc学长的模拟题,T1直接可以暴力破解,T2二分,O(nlog^2n)开始二分写残了,调了半天唉,现 ...

  10. 洛谷P3386 【模板】二分图匹配

    匈牙利算法模板 /*by SilverN*/ #include<algorithm> #include<iostream> #include<cstring> #i ...