【发布时间】:2015-12-05 21:40:59
【问题描述】:
我正在尝试测试一个非常(且非逻辑)的 ruby 类。
class Post
attr_accessor :title
def initialize
@title = "Treehouse Blog"
end
end
class Blog
def create_and_get_title
post = Post.new
post.title
if post.title == nil
post2 = Post.new
post2.title
else
post.title
end
end
end
这是我的测试:
require 'minitest/autorun'
require_relative 'blog'
require 'ostruct'
class TestBlog < Minitest::Test
def setup
@blog = Blog.new
end
def test_title_is_treehouse_blog
assert_equal('Treehouse Blog', @blog.create_and_get_title)
end
def test_title_is_yyy
def Post.new; OpenStruct.new(title: nil) end
def Post.new; OpenStruct.new(title: 'yyy') end
assert_equal('yyy, @blog.create_and_get_title)
end
end
简单吧?但是,我通过运行测试得到以下输出:
1) Failure:
TestBlog#test_title_is_treehouse [test.rb:12]:
Expected: "Treehouse Blog"
Actual: "yyy"
我没有在每次运行中都遇到失败,我只是随机得到它,看起来存根正在被缓存之类的。
有什么想法吗?
非常感谢!
【问题讨论】:
-
您在
assert_equal调用中的test_title_is_yyy方法中有语法错误。缺少报价。