0

TDD

gem 'minitest', '~> 5.2'
require 'minitest/autorun'
require 'minitest/pride'
require_relative 'santa'

class SantaTest < Minitest::Test

 def test_santa_fits_down_the_chimney
  santa = Santa.new
  assert santa.fits?, "Santa fits down the chimney"
 end

 def test_if_santa_eats_too_many_cookies_he_does_not_fit_down_the_chimney

  santa = Santa.new
  santa.eats_cookies
  assert santa.fits?, "He still fits"

  santa.eats_cookies
  assert santa.fits?, "It's a bit of a sqeeze"

  santa.eats_cookies
  refute santa.fits?, "Good thing his suit is stretchy or that wouldn't 
  fit in that either"
 end
end

CODE

class Santa
 attr_reader :eats_cookies

 def initialize
  @eats_cookies = eats_cookies
 end

 def fits?
  true unless @eats_cookies > 2
 else
  false
 end
end

Any direction as to what I can write to get the test to pass on the last test? I'm having an issue organizing the if/then, unless/else statements to make the test pass.

Am I on the right track or am I far off? Appreciate any help

Community
  • 1
  • 1
cjl85
  • 155
  • 13
  • 2
    two things. First, you have an `attr_reader` for `eats_cookies`, so once that's set in your `initialize` method, you can use the reader to get its value, e.g. `eats_cookies` instead of `@eats_cookies`. Second, your `fits?` predicate could use an `if`/`else`, but why not just `def fits? eats_cookies <= 2; end`? – dinjas May 15 '18 at 22:22
  • 1
    That said, is `@eats_cookies` supposed to be a counter? Looking at your usage of it, it looks like perhaps you'd like a counter to get incremented every time it's called? – dinjas May 15 '18 at 22:27
  • The construct `true unless @eats_cookies > 2 else false end' is heinous. Try just `@eats_cookies <= 2` - the `<=` will do the `true` and `false` for you. – Phlip May 15 '18 at 22:30
  • I got closer to getting the test to pass with the @eats_cookies <= 2, but it still gives me a failure for refute santa.fits – cjl85 May 16 '18 at 03:51
  • How are you assigning `eat_cookies` in `initialize` when you are not passing it in args? – Jagdeep Singh May 16 '18 at 04:29

1 Answers1

1

I'm inferring what you're trying to do here, but I think I would do something like this:

class Santa
  attr_accessor :cookies_eaten

  def initialize
    @cookies_eaten = 0
  end

  def eat_cookies
    self.cookies_eaten += 1
  end

  def fits?
    cookies_eaten <= 2
  end
end
dinjas
  • 2,115
  • 18
  • 23
  • I tried that and still got an undefined methods for eat_cookies and when I add that to the attr_accessor, it gave me a failure for refute santa.fits? – cjl85 May 16 '18 at 03:50
  • I accidentally left out a `self` in the `eat_cookies` method. If it still doesn't work, I'd try printing out the cookies - something like `puts "cookies: #{cookies_eaten}"` inside of your `fits?` method. – dinjas May 16 '18 at 04:29
  • May be because in this definition, it is `eat_cookies` and in your test above, it is `eats_cookies` – Jagdeep Singh May 16 '18 at 04:31
  • Yes, Jagdeep is right, if I change it to eats_cookies, the tests pass. I appreciate the help. And another question, why is the self needed and what does that do? – cjl85 May 16 '18 at 16:05
  • I think this answer explains why you need `self` fairly well: https://stackoverflow.com/a/23448046/716039 – dinjas May 16 '18 at 16:34