[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
相关文章
- 1045 Access denied for user ‘root’@’localhost’ (using password: YES)[通俗易懂]
- VSDX Annotator for mac,Visio 绘图注释工具
- clash for windows编程辅助代理工具附文件
- Cornerstone 4 for Mac(SVN管理工具)
- control音响控制软件Sound Control for Mac 完美兼容版
- VRay6.02 for SketchUp为用户提供更加易用和高效的渲染解决方案
- Topaz Gigapixel AI for Mac(图片无损放大软件)
- ORA-24274: no row exists in the string table for these parameters ORACLE 报错 故障修复 远程处理
- ORA-26746: DDL rule “string”.”string” not allowed for this operation ORACLE 报错 故障修复 远程处理
- ORA-29934: index specified for association is not a domain index ORACLE 报错 故障修复 远程处理
- ORA-30504: system triggers cannot have a FOR EACH ROW clause ORACLE 报错 故障修复 远程处理
- ORA-40261: input data for model build contains negative values ORACLE 报错 故障修复 远程处理
- ORA-01107: database must be mounted for media recovery ORACLE 报错 故障修复 远程处理
- ORA-13664: A task-managed “SQL Tuning Set” already exists for task “string”. ORACLE 报错 故障修复 远程处理
- ORA-13717: Tuning Package License is needed for using this feature. ORACLE 报错 故障修复 远程处理
- ORA-13919: Cannot specify values for parameter “string” and for parameter “string” ORACLE 报错 故障修复 远程处理
- mysql 设置编码 Incorrect string value: ‘/xE9/x98/xBF/xE4/xB8/x89…’ for column ‘cont,mysql乱码详解数据库
- 目录用For循环创建Linux目录的简单技巧(for循环创建linux)
- 技巧使用Oracle的FOR循环加快编程效率(oracle的for循环)
- Oracle OJVM:学习和使用的最佳实践(Best Practices for Learning and Using Oracle OJVM)(oracleojvm)
- Maximizing System Performance: A Guide to Using Linux SAR for Monitoring and Analysis.(linuxsar)
- Exploring the Benefits of Using MySQL INT for Database Storage and Retrieval(mysqlint)
- Oracle:学习如何使用For遍历(oracle for遍历)
- 使用For语句操作Oracle数据库(for语句 oracle)
- 遍历Oracle中用For反向遍历字符串的简单示例(oracle中for反向)