[Unit Testing for Zombie] 06. Using Factory
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的更多相关文章
- Unit testing Cmockery 简单使用
/********************************************************************** * Unit testing Cmockery 简单使用 ...
- [Mockito] Spring Unit Testing with Mockito
It is recommened to write unit testing with Mockito in Spring framework, because it is much faster w ...
- Unit Testing of Spring MVC Controllers: “Normal” Controllers
Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- Unit Testing of Spring MVC Controllers: Configuration
Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- Java Unit Testing - JUnit & TestNG
转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignifican ...
- Unit Testing with NSubstitute
These are the contents of my training session about unit testing, and also have some introductions a ...
- [Java Basics3] XML, Unit testing
What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...
- Javascript单元测试Unit Testing之QUnit
body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; } QUnit是一个基于JQuery的单元测试Uni ...
- [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 ...
随机推荐
- 记录一下最近犯得sb的翻车错误
首先是: 数据范围是long long范围,然后写了一个暴力,觉得过不去,于是开了int范围,最后写了个能骗过所有数据的骗分,然后没开longlong... 接着是: for(int i = l; i ...
- linux——(8)数据流重定向、管道命令
概念一:数据流重定向 数据流分输入流和输出流,还有一个标准错误流,负责管理出错信息,比如一般的命令的输出会输出到屏幕上,我们可以用重定向让他输入到某个文件内. 相关操作: 1,标准输入(stdin): ...
- EAP-MD5认证暴力破解工具eapmd5pass
EAP-MD5认证暴力破解工具eapmd5pass EAP-MD5是一种基于802.1x协议的认证机制.由于该机制存在漏洞,所以并不能保证数据安全.Kali Linux预置一个专用工具eapmd5 ...
- Appium robotframework-appium (ios 客户端测试)环境搭建
一. 简介 1.1摘要 本人测试新人,最近在搞ios客户端的自动化,准备采用robotframework-appium来实现自动化测试,一边学习一边总结,此安装说明文档是基于mac系统10.11版本, ...
- 初识Linux 基础操作(2)
1.Linux启动流程 1).linux启动过程 ①.进入grub界面选择相应的启动内核 ②.读取kernel内核文件-/boot/vmlinuz-* ...
- (转载)打破某些大牛比较呵呵的MySQL无file权限读root hash的谣言
如题.比如乌云社区发帖的这位大牛http://zone.wooyun.org/content/12432 看那帖子标题就很喜感有木有,大概意思就是创建了一个没有file权限的账户test,然后不能lo ...
- MySql - GROUP BY 和 HAVING关键字
本文主要针对GROUP BY 和 HAVING两个关键字进行分析 使用测试表如下: 测试前,只知道GROUP BY 这个关键字的作用是根据一个字段进行分组,去重.HAVING对分组设置条件. 具体分组 ...
- 03-MyBatis主从实现代码读写分离应用以及实现
建立目录结构:
- 51nod 1040 最大公约数之和 欧拉函数
1040 最大公约数之和 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1040 Description 给 ...
- python gensim的第一次试用
参考于 http://blog.csdn.net/xiaoquantouer/article/details/53583980 有一个地方很重要,一定要安装anaconda,安装库简直不要太方便. 先 ...