[Unit Testing for Zombie] Using Shoulda to clean up the code
SETUP METHOD
Refactor the following code using a setup method to create the tweet, and properly use instance variables.
class TweetTest < ActiveSupport::TestCase test "invalid without a status" do tweet = tweets(:hello_world) tweet.status = nil assert !tweet.valid?, "Status is not being Validated" end test "valid with all attributes" do tweet = tweets(:hello_world) assert tweet.valid?, "tweet isn't valid" end end
Answer:
class TweetTest < ActiveSupport::TestCase def setup @tweet = tweets(:hello_world) end test "invalid without a status" do @tweet.status = nil assert !@tweet.valid?, "Status is not being Validated" end test "valid with all attributes" do assert @tweet.valid?, "tweet isn't valid" end end
CUSTOM ASSERT
Refactor the following tests to use our custom assert methodassert_attribute_is_validated to test attribute validations.
class TweetTest < ActiveSupport::TestCase def setup @tweet = tweets(:hello_world) end # Don't change this, use it to refactor the tests below. def assert_attribute_is_validated(model, attribute) # This line sets the specified attribute to nil model.assign_attributes(attribute => nil) assert !model.valid?, "#{attribute.to_s} is not being validated" end test "invalid without a status" do @tweet.status = nil assert !@tweet.valid?, "Status is not being Validated" end test "invalid without a zombie" do @tweet.zombie = nil assert !@tweet.valid?, "zombie is not being Validated" end end
Answer:
class TweetTest < ActiveSupport::TestCase def setup @tweet = tweets(:hello_world) end # Don't change this, use it to refactor the tests below. def assert_attribute_is_validated(model, attribute) # This line sets the specified attribute to nil model.assign_attributes(attribute => nil) assert !model.valid?, "#{attribute.to_s} is not being validated" end test "invalid without a status" do @tweet.status = nil assert_attribute_is_validated(@tweet, "status") end test "invalid without a zombie" do @tweet.zombie = nil assert_attribute_is_validated(@tweet, "zombie") end end
TEST HELPER
Move the assert_attribute_is_validated method into the test_helper.rb so that all courses have access to it.
test/models/tweet_test.rb
class TweetTest < ActiveSupport::TestCase def setup @tweet = tweets(:hello_world) end def assert_attribute_is_validated(model, attribute) model.assign_attributes(attribute => nil) assert !model.valid?, "#{attribute.to_s} is not being validated" end #you don't need to touch this test "invalid without a status" do assert_attribute_is_validated @tweet, :status end #you don't need to touch this test "invalid without a zombie" do assert_attribute_is_validated @tweet, :zombie end end
Answer:
test/models/tweet_test.rb
class TweetTest < ActiveSupport::TestCase def setup @tweet = tweets(:hello_world) end #you don't need to touch this test "invalid without a status" do assert_attribute_is_validated @tweet, :status end #you don't need to touch this test "invalid without a zombie" do assert_attribute_is_validated @tweet, :zombie end end
test/test_helper.rb (global file, can be accessed from other test files)
class ActiveSupport::TestCase def assert_attribute_is_validated(model, attribute) model.assign_attributes(attribute => nil) assert !model.valid?, "#{attribute.to_s} is not being validated" end end
INTRODUCING SHOULDA
Using shoulda, test to make sure status and zombie are present.
class Tweet < ActiveRecord::Base belongs_to :zombie validates :status, presence: true, length: {within: 3..140, allow_blank: true} validates :zombie, presence: true def brains? status =~ /(brains|breins)/i end def show_author_summary self.status = self.zombie.zombie_summary end def status_image image = ZwitPic.get_status_image(self.id) # This is returning an array {image_name: image[0], image_url: image[1]} end end
Answer:
class TweetTest < ActiveSupport::TestCase should validate_presence_of(:status) should validate_presence_of(:zombie) end
SHOULDA I
Using shoulda, test to make sure tweet.id is a number and a unique value.
class TweetTest < ActiveSupport::TestCase should validate_uniqueness_of(:id) should validate_numericality_of(:id) end
SHOULDA II
Using shoulda, test to make sure tweet.status has to be between 3 and 140 characters in length.
class TweetTest < ActiveSupport::TestCase should ensure_length_of(:status).is_at_least(3).is_at_most(140) 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工具