单元测试 assert_equal 错误

Unit testing assert_equal error

刚开始学习Ruby,想请教有关单元测试assert_equal功能的问题。我写了我的第一个 class 并尝试用我老师发给我的文件来测试它。当我用自己的程序测试代码时,一切正常,但是当用我老师的程序测试时,我在测试行时经常出错:

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

为什么我得到的不是 "Jan Kowalski"?

问题是,在您的 Person class 中,to_s 没有 return 任何值 - 它只是打印出来!

考虑更改:

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

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

祝你好运!