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. 关于Hibernate中的临时态, 持久态, 游离态

    三态的基本概念: 1, 临时状态(Transient):也叫自由态,只存在于内存中,而在数据库中没有相应数据.用new创建的对象,它没有持久化,没有处于Session中,处于此状态的对象叫临时对象: ...

  2. Python的扩展接口[3] -> Matlab引擎 -> 使用 Python 调用 Matlab 程序

    Python - Matlab 目录 Python-Matlab 引擎 Python-Matlab 数组 Python-Matlab 基本操作 Python-Matlab 调用 m 文件 Matlab ...

  3. DP 题集 2

    关于 DP 的一些题目 String painter 先区间 DP,\(dp[l][r]\) 表示把一个空串涂成 \(t[l,r]\) 这个子串的最小花费.再考虑 \(s\) 字符串,\(f[i]\) ...

  4. Unity 游戏开发技巧集锦之使用忍者飞镖创建粒子效果

    Unity 游戏开发技巧集锦之使用忍者飞镖创建粒子效果 使用忍者飞镖创建粒子效果 游戏中,诸如烟.火.水滴.落叶等粒子效果,都可以使用粒子系统(particle system)来实现.例如,<明 ...

  5. [转]iOS开发new与alloc/init的区别

    1.在实际开发中很少会用到new,一般创建对象咱们看到的全是[[className alloc] init] 但是并不意味着你不会接触到new,在一些代码中还是会看到[className new], ...

  6. herbinate 数据库乱码

    改jdbc或者hibernate编码:   jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=utf-8    ...

  7. 【枚举】【DFS序】Gym - 101617G - Rainbow Roads

    题意:一颗树,每条边有个颜色,一条路径被定义为“彩虹”,当且仅当其上没有长度大于等于2的同色子路径.一个结点被定义为“超级结点”,当且仅当从其发出的所有路径都是“彩虹”. 枚举所有长度为2,且同色的路 ...

  8. 洛谷P1462 通往奥格瑞玛的道路

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  9. ECMAScript5严格模式

    ECMAScript5引入了严格模式(strict mode)的概念,IE10+开始支持.严格模式为JavaScript定义了一种不同的解析和执行模型,在严格模式下,ECMAScript3中的一些不确 ...

  10. Windows系统默认调试器设置

    Windows系统默认调试器设置 1.使用运行打开drwtsn32 -i 注册华生医生到注册表如图: 2.使用运行打开drwtsn32可以进行一些常用的设置如图: 3. 注册表设置: HKEY_LOC ...