我正在努力为我的Tube System程序编写代码和测试,使用Rspec和Ruby编写TDD

时间:2014-12-14 12:19:56

标签: ruby rspec

我正在为火车系统编写一个小程序。

我有乘客,长途汽车,火车和火车站(因此,每个都有一个规格测试)。

我对乘客班的测试是这样的:

let (:passenger) {Passenger.new}

  it "should not be touched in to a station when initialized" do
    expect(passenger.touchedin?).to be false
  end

  it "should be able to enter coach" do
    coach = Coach.new
    passenger.enter(coach)
    expect{coach.to receive(:enter)}
  end

  it "should be able to alight coach" do
    coach = Coach.new
    passenger.alight(coach)
    expect{coach.to receive(:alight)}
  end

  it "should be able to touch into station" do
    station = Station.new
    passenger.touchin(station)
    expect{station.to receive(:touchin)}
  end

  it "should be able to touch out of station" do
    station = Station.new
    passenger.touchout(station)
    expect{station.to receive(:touchout)}
  end
end

我的乘客班就像这样(目前:p):

class Passenger

  def initialize
    @touchedin = false
  end

  def enter(coach)

  end

  def touchedin?
    @touchedin
  end

  def alight(coach)

  end

  def touchin(station)

  end

  def touchout(station)

  end
end

我不确定如何满足我的测试,如果我的测试首先是正确的。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

你并没有真正说出你如何对教练和乘客之间的关系进行建模,但我能想到的一种方法可能如下。我只是为教练/乘客关系提供了足够的东西(所以没有任何关于触摸的内容,因为这涉及到电台) - 而且我使用最小的语法,但我认为你可以了解什么'发生了。

class Coach
  def initialize
    @passengers = []
  end

  ...
end


class Passenger
  def initialize
    @touched_in = false
  end

  def alight(coach)
    coach.passengers << self.uid # or self, if you want the whole passenger object available
  end

  ...
end 


coach = Coach.new
assert_empty coach.passengers

joe = Passenger.new
refute_includes coach.passengers, joe.uid # or joe
joe.alight(coach)
assert_includes coach.passengers, joe.uid # or joe