简单Elixir游戏服设计-测试驱动?

我们开始要进行牌局游戏了。前面的开发我都是代码与测试交织进行,代码先的时候多点。

但在写测试的时候玩玩发现代码不好测试,又去改代码。所以现在我们改下方式,考虑测试优先。

以测试优先的角度去思考,其实前面所有起进程的工作都过早了。应该还是直接测试数据结构的。

牌局游戏首先要发牌吧,因此我们最先应该测试的是往Seat加牌。

seat_test.exs 增加测试代码

 test "添加牌", %{seat: seat} do
      seat = seat |> Seat.add_cards([{1, 1}, {1, 2}]) |> Seat.add_cards({1,3})
      assert [{1, 1}, {1, 2}, {1, 3}] == seat |> Seat.get_cards
  end

  test "公开牌", %{seat: seat} do
      seat = seat |> Seat.open
      assert Seat.is_open?(seat)
  end

  test "重置", %{seat: seat} do
     seat = seat |> Seat.add_score(10)
                  |> Seat.add_cards({1,1})
                  |> Seat.open
                  |> Seat.reset
      assert 0 == seat |> Seat.get_score
      assert [] == seat |> Seat.get_cards
      refute seat |> Seat.is_open?

  end
seat_test.exs

seat.ex 增加让测试通过的代码,具体不贴了,看git吧

https://github.com/rubyist1982/simple.git

目前想到的只有这么多,稍后需要再改。

原文地址:https://www.cnblogs.com/rubyist/p/7655577.html