[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 ...
随机推荐
- python3 怎么统计英文文档常用词?(附解释)
# coding: utf-8 # In[32]: #import requests #from bs4 import BeautifulSoup #res = requests.get(" ...
- mac系统终端下忽略大小写 与 git自动补全(git auto completion)
1.下载git-completion.bash 并放到home 目录下: curl https://raw.githubusercontent.com/git/git/master/contrib/c ...
- openssl-0.9.8k_WIN32(RSA密钥生成工具
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha openssl-0.9.8k_WIN32(RSA密钥生成工具
- [BZOJ4539][HNOI2016]树(主席树)
4539: [Hnoi2016]树 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 746 Solved: 292[Submit][Status][D ...
- [BZOJ3139][HNOI2013]比赛(搜索)
3139: [Hnoi2013]比赛 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1439 Solved: 719[Submit][Status] ...
- [BZOJ1937][SHOI2004]Mst最小生成树(KM算法,最大费用流)
1937: [Shoi2004]Mst 最小生成树 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 802 Solved: 344[Submit][Sta ...
- [CODE FESTIVAL 2018]Sushi Restaurant
题意:有$n$个人,对每个人,他有$p_i$的概率饥饿值为$x_i$($1\leq i\leq m$),你现在要做$n$盘寿司,每盘寿司有一定的数量,当这$n$个人的饥饿值确定后他们会自己选择最优的( ...
- python3-开发进阶 django-rest framework 中的 版本操作(看源码解说)
今天我们来说一说rest framework 中的 版本 操作的详解 首先我们先回顾一下 rest framework的流程: 请求进来走view ,然后view调用视图的dispath函数 为了演示 ...
- 50.分治算法练习: 二分算法: 2703 奶牛代理商 XII
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 小徐从美国回来后,成为了USACO中国区的奶牛销售代理商,专门出售 ...
- 24.最优布线问题(kruskal算法)
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用 ...