[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 @ 2014-10-30 21:56  Zhentiw  阅读(251)  评论(0)    收藏  举报