2015-03-30 3 views
0

Я только начал изучать Ruby, и я хотел бы задать вопрос об модульной функции assert_equal. Я написал свой первый класс и попытался проверить его с файлом, отправленным мне от моего учителя. Когда я тестирую код с моей программы все работает нормально, но при тестировании с одной из моего учителя я постоянно получаю сообщение об ошибке при тестировании линии:Unit testing assert_equal error

asser_equal('Jan Kowalski', p1.to_s) 

Вот мой код:

class Person 

    @@count=0 

    def initialize(name, surname) 
     @@count+=1 
     @name=name 
     @surname=surname 
     @[email protected]@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 

Почему я получаю вместо «Ян Ковальски»?

ответ

1

Дело в том, что ваш класс Person, to_s не возвращает никакой ценности - он просто печатает его!

Рассмотрим изменение:

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

в

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

Успехов!

+0

Thanks Paweł! Оно работает :) – pangniewu