单元测试assert_equal错误

时间:2015-03-30 11:35:30

标签: ruby unit-testing assertions

我刚开始学习Ruby,我想问一下有关单元测试assert_equal函数的问题。我写了我的第一堂课,并尝试用我老师发给我的文件进行测试。当我用我自己的程序测试代码时,一切正常,但是当我用老师测试时,我在测试行时经常出错:

asser_equal('Jan Kowalski', p1.to_s)

这是我的代码:

class Person

    @@count=0

    def initialize(name, surname)
        @@count+=1
        @name=name
        @surname=surname
        @nr=@@count
    end

    attr_reader :nr
    attr_accessor :name, :surname, :count

    def to_s
        puts "#{@name} #{@surname}"
    end

    def create_string
        print "Hello, my name is "
        to_s
    end
    private :create_string
    def say_hello
        create_string
    end

    def Person.count_people()
        if (@@count == 1)
        puts "You have created #{@@count} person"
        else
        puts "You have created #{@@count} people"
        end
    end
end

这是我的老师测试计划:

require_relative 'person'
require 'test/unit'
require 'stringio'

module Kernel
  def capture_stdout
    out = StringIO.new
    $stdout = out
    yield
    return out
  ensure
    $stdout = STDOUT
  end
end

class TestPerson < Test::Unit::TestCase

  def test_person
    out = capture_stdout do
      Person.count_people
    end
    assert_equal("You have created 0 people\n", out.string)

    p1 = Person.new('Jan', 'Kowalski')
    assert_equal('Jan', p1.name)
    assert_equal('Kowalski', p1.surname)

    assert_respond_to(p1, :to_s)
    assert_equal('Jan Kowalski', p1.to_s)

    assert_equal(1, p1.nr)
    assert_respond_to(p1, :say_hello)
    assert_raise(NoMethodError) { p1.nr = 2 }

    out = capture_stdout do
      p1.say_hello
    end
    assert_equal("Hello, my name is Jan Kowalski\n", out.string)

    assert_raise(NoMethodError) { p1.create_string }

    out = capture_stdout do
      Person.count_people
    end
    assert_equal("You have created 1 person\n", out.string)

    p1.name = 'Janina'
    p1.surname = 'Kowalska'
    assert_equal('Janina', p1.name)
    assert_equal('Kowalska', p1.surname)

    p2 = Person.new('Zbyszek', 'Wielki')
    assert_equal(2, p2.nr)

    out = capture_stdout do
      Person.count_people
    end
    assert_equal("You have created 2 people\n", out.string)
  end

end

这是我得到的错误:

    Loaded suite test_person
Started
Jan Kowalski
F
===============================================================================
Failure:
test_person(TestPerson)
test_person.rb:29:in `test_person'
     26:     assert_equal('Kowalski', p1.surname)
     27:
     28:        assert_respond_to(p1, :to_s)
  => 29:     assert_equal('Jan Kowalski', p1.to_s)
     30:
     31:     assert_equal(1, p1.nr)
     32:     assert_respond_to(p1, :say_hello)
<"Jan Kowalski"> expected but was
<nil>

diff:
? "Jan Kowalski"
?     i
===============================================================================


Finished in 0.018998 seconds.

1 tests, 5 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifica
tions
0% passed

52.64 tests/s, 263.19 assertions/s

为什么我会而不是&#34; Jan Kowalski&#34;?

1 个答案:

答案 0 :(得分:1)

问题是,在Person课程中,to_s没有返回任何值 - 它只会打印出来!

考虑改变:

def to_s
  puts "#{@name} #{@surname}"
end

def to_s
  "#{@name} #{@surname}"
end
祝你好运!