[Unit Testing for Zombie] 01. Test Unit
Unit Test
Write a basic conditional test using assert which checks if 1 > 0. Name your test classConditionalTest.
require 'test/unit' class ConditionalTest < Test::Unit::TestCase def test_conditional assert 1 > 0 end end
ERROR MESSAGES
Add the custom error message One is not greater than zero to the failing assertion we just created.
class ConditionalTest < Test::Unit::TestCase def test_one_greater_than_zero assert 0 > 1, "One is not greater than zero" end end
TEST DRIVEN DEVELOPMENT
In a moment we are going to create the multiple_of? in our Multiple module which returns true if a number is a multiple of another number. Finish the test below, asserting thatMultiple.multiple_of?(10, 5) returns true.
class MultipleTest < Test::Unit::TestCase def test_multiple_of assert Multiple.multiple_of?(10, 5) end end
WRITE THE METHOD
Now that we have a failing test, let's make it pass by creating the self.multiple_of?(multiple, num) method in the Multiple module. Hint: one way of checking multiples is using the modulo operator, multiple % num == 0 (will be 'true' if multiple can be divided evenly by the num) Modulo on Wikipedia
module Multiple def self.multiple_of?(multiple, num) multiple % num == 0 end end
ASSERT_NOT_NIL
We are going to create a Zombifier class with a zombify method that upcases and adds'BRAINS'. Lets begin by writing a test using assert_not_nil to make sure zombify returns something
zombifier.rb
class Zombifier def initialize(str) @str = str end def zombify @str end end
test/unit/zombifier_test.rb
class ZombifierTest < Test::Unit::TestCase def test_zombify_returns_something z = Zombifier.new('make me a zombie') assert_not_nil z.zombify, 'zombify is returning nil' end end
ASSERT_MTACH
Notice our zombifier.rb file and how the zombify method adds 'BRAINS' to the string, useassert_match to test if zombify is doing this correctly.
class Zombifier def initialize(str) @str = str end def zombify "#{@str} BRAINS" end end
Answer:
class ZombifierTest < Test::Unit::TestCase def test_zombify_brains z = Zombifier.new('I love your arms') assert_match(/BRAINS/, z.zombify, 'print zombie + BRAINS') end end
ASSERT_EQUAL
Now, using assert_equal, write a test to make sure zombify returns the expected string,upcase with 'BRAINS'. ex. "HELLO WORLD BRAINS" is expected when we call Zombifier.new("Hello world").zombify
class ZombifierTest < Test::Unit::TestCase def test_zombify_upcase assert_equal("HELLO WORLD BRAINS", Zombifier.new("Hello world").zombify, 'zombify not upcase the string') end end
ASSERT_RAISE
Notice in zombifier.rb how we now raise a RuntimeError if the string already looks like a zombie (contains 'BRAINS'). Test for this behavior using assert_raise.
class Zombifier def initialize(str) @str = str end def zombify raise RuntimeError if @str =~ /BRAINS/ "#{@str.upcase} BRAINS" end end
Answer:
class ZombifierTest < Test::Unit::TestCase def test_brains_in_zombify_raises_error z = Zombifier.new('BRAINS') assert_raise(RuntimeError) {z.zombify} end end
or
class ZombifierTest < Test::Unit::TestCase def test_brains_in_zombify_raises_error z = Zombifier.new('BRAINS') assert_raise RuntimeError do z.zombify end end end
ASSERT_KIND_OF
Since zombify is supposed to modify an existing string, it should also return a string. Create a test using assert_kind_of to make sure a String is being returned.
class ZombifierTest < Test::Unit::TestCase def test_zombify_returns_a_string z = Zombifier.new('I like knees') assert_kind_of(String, z.zombify, 'Zombify does not return a string') end end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具