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. Mybatis源码分析之结果集处理

    解析封装 ResultMap 是和结果集相关的东西,最初在解析 XML 的时候,于 parseStatementNode 方法中,针对每一个 select 节点进行解析,转换为 MappedState ...

  2. logging记录日志

    日志是一个系统的重要组成部分,用以记录用户操作.系统运行状态和错误信息.日志记录的好坏直接关系到系统出现问题时定位的速度.logging模块Python2.3版本开始成为Python标准库的一部分. ...

  3. linux——(3)文件与目录管理

    文件与目录管理相关指令 ls [-adlR] 目录 #查看目录与文件的命令. -a #连同隐藏文件一起列出来. -d #只列出目录. -l #列出相关属性和权限等数据. -R #连同子目录内容一起列出 ...

  4. 趴一趴京东的Ajax动态价格页面

    AJAX,异步加载技术!!! 之前在网上看过很多朋友有一种疑问,为什么在看京东网页的源代码里面看不到价格或则折扣一类的数据,而在网页上正常显示却能看到?...之前我也没有想到是AJAX,因为我写写爬虫 ...

  5. php基础知识一

    1.PHP是什么: 开源,免费的,跨平台的 2.PHP能做什么: 3.PHP的特点: 4.PHP的标记风格: <?php ?> <? ?> <script languag ...

  6. BZOJ1975 SDOI2010魔法猪学院

    就是个A*,具体原理可以参考VANE的博文. 正解要手写堆,会被卡常,也许哪天我筋搭错了写一回吧. #include<bits/stdc++.h> #define r register u ...

  7. MySQL注射绕过技巧(三)

    在测试一次注入的时候发现过滤了逗号 所以找到这个思路 第一次遇到的时候是看key哥挖洞  遇到后就想记录下来 正文 过滤了逗号  利用join来逐步查询 select*from(select 1)a ...

  8. [GCJ2017R2]Fresh Chocolate

    题目大意: 有n个团和很多盒糖,每个团中人数不一定相同,每盒糖中都有p颗糖. 现在要给每个团发糖,要求每个人都要发到糖,只有一盒糖发完后才能发下一盒糖. 发糖的顺序可以任意安排,问经过合理安排后,最多 ...

  9. 【点分治】【路径小于等于k的条数】【路径恰好等于k是否存在】

    POJ1741:Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29574   Accepted: 9915 Des ...

  10. Codeforces Round #301 (Div. 2) D. Bad Luck Island 概率DP

    D. Bad Luck Island Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/pr ...