pangniewu
pangniewu

Reputation: 3

Unit testing assert_equal error

I just started to learn Ruby and I'd like to ask question about unit testing assert_equal function. I wrote my first class and tried to test it with the file sent to my from my teacher. When I'm testing the code with my own program everything works fine but when testing with the one from my teacher I constantly get an error when testing line:

asser_equal('Jan Kowalski', p1.to_s)

Here is my code:

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

Here's my teacher testing program:

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

And here's the error that i get:

    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

Why do I get instead of "Jan Kowalski"?

Upvotes: 0

Views: 364

Answers (1)

Paweł Dawczak
Paweł Dawczak

Reputation: 9639

The thing is, in your Person class, to_s doesn't return any value - it just prints it!

Consider changing:

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

to

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

Good luck!

Upvotes: 1

Related Questions