[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 ...
随机推荐
- 【JAVAWEB学习笔记】30_WEB总结_思维导图
可以在浏览器放大来查看细节,或者另存为图片到本地电脑查看.
- 【BZOJ 3996】 3996: [TJOI2015]线性代数 (最小割)
3996: [TJOI2015]线性代数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1368 Solved: 832 Description 给 ...
- 可以在GitHub或者码云里 直接搜索 项目 比如 哔哩哔哩
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha Search · 哔哩哔哩 哔哩哔哩 · 搜索 - 码云 还有就是 以前的项目 可以不要 ...
- BZOJ 2434 [Noi2011]阿狸的打字机(AC自动机)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2434 [题目大意] 给出一个打印的过程,'a'-'z'表示输入字母,P表示打印该字符串 ...
- bzoj 3809 莫队
收获: 1.分块时顺便记录每个位置所属的块,然后一次排序就OK了. 2.要权衡在“区间移动”与“查询结果”之间的时间,莫队算法一般区间移动频率远大于查询结果,所以我们选择的辅助数据结构时就要注意了,我 ...
- SQL的in的参数化查询
SqlCommand cmd=con.CreateCommand(); cmd.CommandText="exec('select * from novel where novelid in ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp
E. Intellectual Inquiry 题目连接: http://www.codeforces.com/contest/655/problem/E Description After gett ...
- Matlab 矩阵【Mark】
一.矩阵的表示在MATLAB中创建矩阵有以下规则: a.矩阵元素必须在”[ ]”内: b.矩阵的同行元素之间用空格(或”,”)隔开: c.矩阵的行与行之间用”;”(或回车符)隔开: d.矩阵的元素可以 ...
- 安装与使用adb
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ...
- 前后端常用通讯方式-- ajax 、websocket
一.前后端常用通讯方式 1. ajax 浏览器发起请求,服务器返回数据,服务器不能主动返回数据,要实现实时数据交互只能是ajax轮询(让浏览器隔个几秒就发送一次请求,然后更新客户端显示.这种方式实际 ...