FACTORIES

Convert the zombies fixture to a Factory Girl Factory called :zombie.

test/fixture/zombies.yml

zombie:
name: 'Sally'
graveyard: 'Valley Dim'

Answer:

test/factories/zombies.rb

FactoryGirl.define do
factory :zombie do
name 'Sally'
graveyard 'Valley Dim'
end
end

Why using factory instead of fixture?

COMPLEX FACTORIES

Use a nested Factory Girl definition to create zombie factories named :sally and :moe, using the data from the fixtures below.

test/fixtures/zombies.yml

ash:
name: 'Ash'
graveyard: 'Petrosville' sally:
name: 'Sally'
graveyard: 'Valley Dim' moe:
name: 'Moe'
graveyard: 'Petrosville'

Answer:

test/factories/zombies.rb

FactoryGirl.define do
factory :zombie do
name 'Ash'
graveyard 'Petrosville' # Add sally and moe here
factory :sally do
name 'Sally'
graveyard 'Valley Dim'
end factory :moe do
name 'Moe'
end
end
end

How to create a factory?

What's good in factory?

We can use nested factory to create new factory and reused common part.

UNIQUE ATTRIBUTES

Refactor the zombie factory using a sequence so that we get a unique name and graveyardeverytime we create a new zombie.

FactoryGirl.define do
factory :zombie do
sequence(:name) {|i| "Ash#{i}" }
sequence(:graveyard) { |j| "Petrosville Cemetary#{j}" }
end
end

Why using sequence?

Every time using a Factory, it equals to :

FactoryGirl.create(:zombie) //create new zombie instance and save into db

But if the data should be uniqueness, then it will cause some problem like: ActiveRecord: RecordInvalid.

Then we can use sequence to solve this problem.

ASSOCIATIONS

Create a tweet factory with a zombie association, don't forget to set a tweet status.

FactoryGirl.define do
factory :zombie do
name 'Sally'
graveyard 'Valley Dim'
end
end

Answer:

FactoryGirl.define do
factory :tweet do
status "Eat a brain"
association :zombie
end
end

What about data relationship?

USING FACTORIES

Using factories write a test to verify that a tweet is invalid without a status. Be sure tobuild the Tweet, rather than create it.

FactoryGirl.define do
factory :tweet do
association :zombie
status "Need brain factory."
end
end

Answer:

class TweetTest < ActiveSupport::TestCase
test "A tweet requires a status" do
tweet = Factory.build(:tweet, status: nil)
assert !tweet.valid?, "Status is not being validated"
end
end

USING FACTORIES II

Create a tweet using a factory. Then, using Capybara, go to the tweets_url, click on thetweet.status link. Finally, assert that the tweet's show page contains the@tweet.zombie.name in its h3. Use Capybara's within and has_content? methods.

//index.html

<ul class="tweets">
<li><%= link_to @tweet.status, tweets_url(@tweet) %></li>
</ul>
//show.html

<div id='<%="tweet_#{@tweet.id}"%>'>
<h3><%= @tweet.zombie.name %></h3>
<p><%= @tweet.status %></p>
</div>

test/factories/tweets.rb

FactoryGirl.define do
factory :tweet do
association :zombie
status "Need brain factory."
end
end

factories/zombies.rb

FactoryGirl.define do
factory :zombie do
name "Ash"
graveyard "Factory Hills Cemetary"
end
end

Answer:

class TweetTest < ActionDispatch::IntegrationTest
test "tweet page has zombie link" do
tweet = Factory(:tweet)
visit tweets_url
click_link tweet.status within("h3") do
assert has_content? (tweet.zombie.name)
end
end
end

[Unit Testing for Zombie] 06. Using Factory的更多相关文章

  1. Unit testing Cmockery 简单使用

    /********************************************************************** * Unit testing Cmockery 简单使用 ...

  2. [Mockito] Spring Unit Testing with Mockito

    It is recommened to write unit testing with Mockito in Spring framework, because it is much faster w ...

  3. Unit Testing of Spring MVC Controllers: “Normal” Controllers

    Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  4. Unit Testing of Spring MVC Controllers: Configuration

    Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  5. Java Unit Testing - JUnit & TestNG

    转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignifican ...

  6. Unit Testing with NSubstitute

    These are the contents of my training session about unit testing, and also have some introductions a ...

  7. [Java Basics3] XML, Unit testing

    What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...

  8. Javascript单元测试Unit Testing之QUnit

    body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; }           QUnit是一个基于JQuery的单元测试Uni ...

  9. [Unit Testing] AngularJS Unit Testing - Karma

    Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. A ...

随机推荐

  1. Python函数系列-迭代器,生成器

    一 迭代器 一 迭代的概念 #迭代器即迭代的工具,那什么是迭代呢?#迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 while True: #只是单纯地重复,因而不 ...

  2. 分布式锁的理解,java自带的锁为什么会失效

    前段时间在发送短信的代码块上通过网上找的工具类基于Redis实现了分布式锁的功能 对应的链接https://www.cnblogs.com/c-h-y/p/9391602.html 周末想细细看一下. ...

  3. 连接LilyPad之Linux平台的驱动

    连接LilyPad之Linux平台的驱动 常用的Linux发行版都自带了FTDI驱动,因此在绝大多数Linux发行版中不需要用户进行额外的操作. 在LilyPad编程器被正确驱动后,就可以将LilyP ...

  4. Sea Battle<海战>(思路题)

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. Scrapy实战篇(五)爬取京东商城文胸信息

    创建scrapy项目 scrapy startproject jingdong 填充 item.py文件 在这里定义想要存储的字段信息 import scrapy class JingdongItem ...

  6. [BZOJ4237]稻草人(CDQ分治)

    先按y排序,二分,两边递归下去,然后处理下半部分对上半部分的贡献,即左下点在下半部分,右上点在上半部分的合法矩形个数. 两个部分均按x排序,枚举右上点p,则左下点需要满足: 1.横坐标大于上半部分纵坐 ...

  7. Mybatis 删除多条数据XML SQL语句删除

    Mybatis 删除多条数据XML SQL语句删除 1.删除多条数据SQL写法 <delete id="deleteParamsByIds"> delete from ...

  8. trie--- POJ 3764 The xor-longest Path

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5453   Accepted: 1 ...

  9. MyEclipse2015创建配置Web+Maven项目

    首先我的MyEclipse版本是2015 stable 2.0,在MyEclipse中创建Maven项目通常有两种常见的方式,它们分别是: New Maven Project  New Web Pro ...

  10. java中的容器解释

    解释一:容器(Container)Spring 提供容器功能,容器可以管理对象的生命周期.对象与对象之间的依赖关系,您可以使用一个配置文件(通常是XML),在上面定义好对象的名称.如何产生(Proto ...