Having trouble with how classes work

时间:2015-05-12 22:15:45

标签: ruby-on-rails ruby

I'm testing this code for a class assignment to make sure it works. I don't quite understand code yet. Maybe give me some pointers.

public class UserRepositoryImpl<T> implements IUserRepository<T> {

@PersistenceContext
private EntityManager em;

private Class< T > type;

@Override
public List<T> findAllValidEtSiteAndStructure() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication instanceof UserAuthentication) {
        final User currentUser = ((UserAuthentication) authentication).getDetails();
        return (List<T>) em.createQuery("FROM " + type.getName()+ " WHERE site=:site AND structure=:structure AND valid=:valid")
                .setParameter("site", currentUser.getInstallation().getSite())
                .setParameter("structure", currentUser.getInstallation().getStructure())
                .setParameter("valid", true)
                .getResultList();
    }
    return null;
}
}

Here's what I've added to it.

require 'minitest/autorun'

require './human'

require './coffee'

## This assignment made more sense when I wrote it in the morning.
class CaffeineTest < MiniTest::Test
  def test_humans_tend_to_be_sleepy
    tyler = Human.new "Tyler"
    assert tyler.alertness < 0.1
  end

  def test_humans_need_coffee
    randy = Human.new "Randy"
    refute randy.has_coffee?
    assert randy.needs_coffee?
  end

  def test_humans_can_drink_coffee
    sherri = Human.new "Sherri"
    tsmf = Coffee.new "Triple Shot Mocha Frappuccino"
    assert tsmf.full?

    sherri.buy tsmf
    sherri.drink!
    assert_in_epsilon sherri.alertness, 0.33, 0.1
    refute tsmf.full?
    refute tsmf.empty?
  end

  def test_humans_can_drink_all_the_coffee
    trevor = Human.new "Trevor"
    tsmf = Coffee.new "Triple Shot Mocha Frappuccino"
    trevor.buy tsmf

    3.times { trevor.drink! }
    assert tsmf.empty?
    assert trevor.alertness > 0.9
  end
end

I keep getting failures on class Human def initialize(name) @name = name @alertness = 0.0 @flavor = Coffee.new(name) @coffee = 0 end def alertness @alertness end def has_coffee?() false end def needs_coffee?() true end def drink!() @alertness += 0.33 @coffee -= 0.33 end def empty? if @coffee <= 0 true else false end end def buy(type) @flavor @coffee += 1 #add tsmf to some kind of array or some shit end end class Coffee def initialize(drink) @coffee = 0 end def full? if @coffee >= 0 true else false end end def has_coffee? false end end and empty?. Please guide me a little here.

1 个答案:

答案 0 :(得分:0)

require 'minitest/autorun'


## This assignment made more sense when I wrote it in the morning.
class CaffeineTest < MiniTest::Test
  def test_humans_tend_to_be_sleepy
    tyler = Human.new "Tyler"
    assert tyler.alertness < 0.1
  end

  def test_humans_need_coffee
    randy = Human.new "Randy"
    refute randy.has_coffee?
    assert randy.needs_coffee?
  end

  def test_humans_can_drink_coffee
    sherri = Human.new "Sherri"
    tsmf = Coffee.new "Triple Shot Mocha Frappuccino"
    assert tsmf.full?

    sherri.buy tsmf
    sherri.drink!
    assert_in_epsilon sherri.alertness, 0.33, 0.1
    refute sherri.empty?
  end

  def test_humans_can_drink_all_the_coffee
    trevor = Human.new "Trevor"
    tsmf = Coffee.new "Triple Shot Mocha Frappuccino"
    trevor.buy tsmf

    3.times { trevor.drink! }
    assert tsmf.empty?
    assert trevor.alertness > 0.9
  end
end


class Human
  def initialize(name)
    @name = name
    @alertness = 0.0
    @flavor = Coffee.new(name)
    @coffee = 0
  end

  def alertness
    @alertness
  end

  def has_coffee?()
    false
  end

  def needs_coffee?()
    true
  end

  def drink!()
    @alertness += 0.33
    @coffee -= 0.33
  end

  def empty?
    if @coffee <= 0
      true
    else
      false
    end
  end


  def buy(type)
    @flavor
    @coffee += 1 #add tsmf to some kind of array or some shit
  end

end

class Coffee
  def initialize(drink)
    @coffee = 0
  end

  def full?
      if @coffee >= 0
        true
      else
        false
      end
  end

  def has_coffee?
    false
  end

  def empty?
    if @coffee <= 0
      true
    else
      false
    end
  end


end