#DND error - ruby "undefined method -"

3 messages · Page 1 of 1 (latest)

humble violet
#

Test Error:
NoMethodError: undefined method `-' for nil

Traceback (most recent call first):
Line 12:in `modifier'

full test:

  def test_random_character_stats
    skip
    100.times do
      character = DndCharacter.new
      allowed_range = (3..18)
      expected_hitpoints = BASE_HITPOINTS +
                           DndCharacter.modifier(character.constitution)
      informative_message = %q(The character's %s must be within %s)
      attributes.each do |attribute|
        assert_includes allowed_range, character.send(attribute),
          informative_message % [attribute, allowed_range]
      end
      informative_message = %q(The character's %s must be %s)
      assert_equal expected_hitpoints, character.hitpoints,
        informative_message % ['hitpoints', expected_hitpoints]
    end
  end

I barely have any code written, not sure how 16 of the 18 tests are passing:

code:

class DndCharacter
  def self.modifier(mod_value = 0)
    puts mod_value
    ((mod_value - 10) / 2).round  
  end

  def constitution
  end

  def initialize
    # Your code here
  end
end

I interpreted the test error to mean that mod_value was nil, so i added the default of 0.

I feel like something is wrong with the test suite on this one, in that they are not leading me to deeper problems in the code. Now I am almost at the end of the problem, from a testing perspective, only 2 tests left & it's not easy to see what is supposed to be next just by looking at the tests.

sharp dagger
#

So - isn’t defined for something we’re calling it in. That something appears to be nil. So the question is where is that nil coming? Can we figure that out?

humble violet
#

it would be the only - that i see is on mod_value - 10, mod_value should be 0 as default