S
S
sumproxy2016-04-22 09:54:14
ruby
sumproxy, 2016-04-22 09:54:14

Why does assert throw an error?

I am doing the kindergarten_garden task on exercism
I am writing the following code

class Garden
  STUDENTS = [:alice,  :bob,    :charlie, :david,
              :eve,    :fred,   :ginny,   :harriet,
              :ileana, :joseph, :kincaid, :larry]

  PLANTS =   {"V" => :violets,
              "C" => :clover,
              "G" => :grass,
              "R" => :radishes}

  def initialize(diagram, students = STUDENTS)
    students = students.map {|s| s.downcase.to_sym }.sort

    row1, row2 = diagram
                 .lines
                 .map(&:chomp)
                 .map(&:each_char)

    row1, row2 = row1.each_slice(2), row2.each_slice(2)

    row1.zip(row2).each_with_index do |plants, idx|
      self.class.send(:define_method,
                      students[idx],
                      -> { plants.flatten.map {|p| PLANTS[p] } })
    end
  end
end

on the test
class TwoGardensDifferentStudents < Minitest::Test
  def diagram
    "VCRRGVRG\nRVGCCGCV"
  end

  def garden_1
    @garden_1 ||= Garden.new(diagram, %w(Alice Bob Charlie Dan))
  end

  def garden_2
    @garden_2 ||= Garden.new(diagram, %w(Bob Charlie Dan Erin))
  end

  def test_bob_and_charlie_per_garden
    assert_equal [:radishes, :radishes, :grass, :clover], garden_1.bob
    assert_equal [:violets, :clover, :radishes, :violets], garden_2.bob
    assert_equal [:grass, :violets, :clover, :grass], garden_1.charlie
    assert_equal [:radishes, :radishes, :grass, :clover], garden_2.charlie
  end
end

crashes with an error
1) Failure:
TwoGardensDifferentStudents#test_bob_and_charlie_per_garden [kindergarten_garden_test.rb:128]:
--- expected
+++ actual
@@ -1 +1 @@
-[:grass, :violets, :clover, :grass]
+[:radishes, :radishes, :grass, :clover]

although printing the value for assert to the console gives the correct value.
I can't understand why.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question