[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

 

posted @   Zhentiw  阅读(244)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 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工具
点击右上角即可分享
微信分享提示