[Unit Testing Zombie] 02. Model Test

First Model Test

Using an assert and the valid? method, test that a 'tweet' is not valid without a status.

复制代码
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
  test "invalid without a status" do
    tweet = Tweet.new
    assert !tweet.valid?, 'not valid without a status'
  end
end

 

Prepare for DB

Execute the rake command which prepares the test database for tests.

rake db:test:prepare

 

Running Tests

Execute the rake command which will run both db:test:prepare and all the tests.

rake test

 

Vlidate with attributes

Lets try another validation test. This time, test to make sure a tweet is valid with all its attributes before save. A tweet has a zombie and a status (you'll need to create a zombie for this).

复制代码
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
  test "valid with all attributes" do
    tweet = Tweet.new
    zombie = Zombie.new
    
    tweet.status = "I am here to eat your brain"
    tweet.zombie = zombie
    
    assert tweet.valid?, "Attributes of tweet is not valid"
  end
end
复制代码

 

 

Fixtures

Create a tweets fixture in the tweets.yml file. The Tweet model has a zombie_id that's an Integer and a status that's a String.

zombies.yml:

ash:
  id: 1
  name: 'Ash'
  graveyard: 'Oak Park Cemetary'

test/fixtures/tweets.yml:

hello world:
    zombie_id: 1
    status: "I am here to eat your brain"

 

TESTS WITH FIXTURES

Now that we have fixtures tweets.yml and zombies.ymlbelow, let's clean up some tests. Add fixtures to the following tests.

zombies.yml:

ash:
id: 1
name: 'Ash'
graveyard: 'Oak Park Cemetary'
 
tweets.yml:
hello_world:
zombie_id: 1
status: "Hello World"
 
复制代码
class TweetTest < ActiveSupport::TestCase
  test "valid with all attributes" do
    zombie = zombies(:ash)
    tweet = tweets(:hello_world)

    tweet.status = 'I want to eat your brain. <3'
    tweet.zombie = zombie

    assert tweet.valid?, "tweet isn't valid"
  end
end
复制代码

 

MODEL METHODS

Create a test that ensures the brains? method returns true if a status contains 'brains'.

复制代码
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
  test "can detect brains" do
    tweet = tweets(:hello_world)
    tweet.status = "I am here to eat your brains"
    assert tweet.brains?, "tweet.brains does not has brains"
  end
end

 

TESTING RELATIONSHIPS

Create a test to ensure that the hello_world tweet contains zombie Ash.

class TweetTest < ActiveSupport::TestCase
  test "contains a zombie" do
    tweet = tweets(:hello_world)
    zombie = zombies(:ash)
    
    assert_equal zombie.name, tweet.zombie.name, "hello_world tweet doesn not contain zombie Ash" 
  end
end

 

posted @   Zhentiw  阅读(335)  评论(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工具
点击右上角即可分享
微信分享提示