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. Poj1741/洛谷P4718 Tree(点分治)

    题面 有多组数据:Poj 无多组数据:洛谷 题解 点分治板子题,\(calc\)的时候搞一个\(two\ pointers\)扫一下统计答案就行了. #include <cmath> #i ...

  2. 使用 Eigen 3.3.3 进行矩阵运算

    Eigen是一个能够进行线性代数运算的C++开源软件包,包含矩阵和矢量操作,Matlab中对矩阵的大多数操作都可以在Eigen中找到. 最近需要计算厄米特矩阵的逆,基于LLT分解和LDLT分解,自己写 ...

  3. ironic简介

    转:https://doodu.gitbooks.io/openstack-ironic 简介 Bare Metal Servcie 裸机服务 -- 'bear betal' ironic简介 如今O ...

  4. 【BZOJ 2460】线性基

    2460: [BeiJing2011]元素 Description 相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力 ...

  5. Codeforces Round #479 (Div. 3)

    手速场2333,这群人贼牛逼!手速贼快!   A. Wrong Subtraction time limit per test 1 second memory limit per test 256 m ...

  6. 【轮廓线DP】POJ2411-Mondriaan's Dream

    今天美国的院士过来讲课XD以为会很无聊但是谜之好听,而且英语基本上都听懂了的样子♪(´▽`) 逃到图书馆来写解题报告 [题目大意] 给出一个m*n的方格,用2*1的骨牌覆盖有几种情况. [思路] 最基 ...

  7. win7环境下一次浅谈栈溢出

    在我们的生活中,存在的许许多多的漏洞,下面像大家介绍的就是平时比较常见的栈溢出漏洞的实践过程. 下面,我们用一个非常简单的例子来让大家对栈溢出漏洞有个直观的认识. 这是一个简单的密码验证程序,但因为代 ...

  8. MySQL -- 性能优化的最佳20+条经验

    FROM:http://www.cnblogs.com/shlhm/p/3235848.html ,学习学习~ 今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库 ...

  9. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题

    A. Bear and Three Balls 题目连接: http://www.codeforces.com/contest/653/problem/A Description Limak is a ...

  10. 集成学习中的 stacking 以及python实现

    集成学习 Ensemble learning 中文名叫做集成学习,它并不是一个单独的机器学习算法,而是将很多的机器学习算法结合在一起,我们把组成集成学习的算法叫做“个体学习器”.在集成学习器当中,个体 ...