[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 ...
随机推荐
- Java synchronized的原理解析
开始 类有一个特性叫封装,如果一个类,所有的field都是private的,而且没有任何的method,那么这个类就像是四面围墙+天罗地网,没有门.看起来就是一个封闭的箱子,外面的进不来,里面的出不去 ...
- 使用JDBC连接数据库的一些BUG
题记:前几天用JDBC连接MYSQL数据库的时候,出现了一些BUG,有代码层次的,也有设置层次的, 下面的解决方法时我目前所遇到的,后期如果还有遇到的会进行补充. 一.出现:远程mysql_java. ...
- PHP视频教程 字符串处理函数(二)
1.字符串输出:echo()输出一个或者多个字符串 2.截取 chunk_split(); 3.
- 【BZOJ 3997】 3997: [TJOI2015]组合数学 (DP| 最小链覆盖=最大点独立集)
3997: [TJOI2015]组合数学 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 919 Solved: 664 Description 给出 ...
- PHP配置文件经典漏洞
phithon师父在小蜜圈里放了一个经典的配置文件写入问题漏洞. <?phpif(!isset($_GET['option'])) die();$str = addslashes($_GET[' ...
- Java并发(七):双重检验锁定DCL
双重检查锁定(Double Check Lock,DCL) 1.懒汉式单例模式,无法保证线程安全: public class Singleton { private static Singleton ...
- List集合多次排序
写在前面: 有时候我们在查询数据展示到前台页面的时候,需要对数据进行排序,特别是按照多个字段进行排序,会很麻烦写的代码也比较多.这个时候java8的特性可以让我们很方便的对数据进行排序. 话不多说,直 ...
- 05-树6. Path in a Heap (25) 小根堆
05-树6. Path in a Heap (25) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.patest.cn/contes ...
- hdu 4112 Break the Chocolate 贪心
Break the Chocolate Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem. ...
- VS2010下面Empty Project使用
VS2010下面Empty Project使用:1,添加代码HelloApp #include <afxwin.h> class CHelloApp:public CWinApp{publ ...