カードゲームを作る　その８（戦闘API　デバッグ）

##この記事は
とある[カードゲーム](http://arcana.vector.co.jp)が非常に面白く、
そのゲームの見た目クローンを作ろうと一人のプログラマーが奮闘していくブログのような記事です。

その７の続き
ここでは、前回テストが通らなかったので、その原因を探るため、デバッグ環境を
準備していきます。

参考サイト：[Railsでデバッグをする](http://ruby-rails.hatenadiary.com/entry/20150108/1420721205)

##デバッグ環境のインストール
```ruby:Gemfileに以下を追加
group :development, :test do
  gem 'pry-rails'  # rails console(もしくは、rails c)でirbの代わりにpryを使われる
  gem 'pry-doc'    # methodを表示
  gem 'pry-byebug' # デバッグを実施
  gem 'pry-stack_explorer' # スタックをたどれる
end
```
```bash:インストール
$ bundle install
:
:
Using rails-api 0.4.0
Using rails 4.2.5
Bundle complete! 8 Gemfile dependencies, 49 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
```

##デバッグ準備
インストールが完了したら、ブレークポイントを打ちたいところで
```binding.pry```を入力します。

```ruby:app/controllers/battles_controller.rb
class BattlesController < ApplicationController
  before_action :set_battle, only: [:show, :update, :destroy]

  # GET /battles
  # GET /battles.json
  def index
    # @battles = Battle.all
    #
    # render json: @battles
    # render json: ["hoge", params[:BType],
    #                       params[:UId], params[:BTarget], params[:V] ]
    _BType = params[:BType]
    _UId   = params[:UId]
    _BTarget = params[:BTarget]
    _V = params[:V]
binding.pry
    _Result = "0"
    case _UId
    when "Kingdom"
：
：
```

##デバッグ実行

```irb:testの実行結果、及び、paramsの確認
$ rake test
Run options: --seed 38166

# Running:

.....
Frame number: 0/43

From: /Urakata/app/controllers/battles_controller.rb @ line 17 BattlesController#index:

    12:     _BType = params[:BType]
    13:     _UId   = params[:UId]
    14:     _BTarget = params[:BTarget]
    15:     _V = params[:V]
    16: binding.pry
 => 17:     _Result = "0"
    18:     case _UId
    19:     when "Kingdom"
    20:       case _BTarget
    21:       when "Kingdom"
    22:         _BWinner = 0

[1] pry(#<BattlesController>)> params
=> {"battle"=>{"BType"=>"arena", "UId"=>"Spiritual", "BTarget"=>"Tribal", "V"=>"1"}, "controller"=>"battles", "action"=>"index"}
```
```ruby:paramsの中身
{"battle"=>{"BType"=>"arena", "UId"=>"Spiritual", "BTarget"=>"Tribal", "V"=>"1"}, "controller"=>"battles", "action"=>"index"}
```

？？paramsの中身を確認すると、思っていた構成と違いました。
developで動かした結果を見ると思ってた構成が返ってくるので、
違いを探っていきます。

```json:developの実行結果
{"Result":"0","BWinner":1,"BProcess":{"Turn":1,"me01":{"t01":{"HP":0,"ATK":1}}}}
```

developをデバッグしてみるとやはり違いました。

```irb:実行結果
$ rails server -p 2222
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://localhost:2222
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-02-02 00:59:49] INFO  WEBrick 1.3.1
[2016-02-02 00:59:49] INFO  ruby 2.1.5 (2014-11-13) [x86_64-darwin14.0]
[2016-02-02 00:59:49] INFO  WEBrick::HTTPServer#start: pid=10318 port=2222


Started GET "/battle/arena/Kingdom/Pluto/1" for ::1 at 2016-02-02 01:00:11 +0900
  ActiveRecord::SchemaMigration Load (0.4ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by BattlesController#index as HTML
  Parameters: {"BType"=>"arena", "UId"=>"Kingdom", "BTarget"=>"Pluto", "V"=>"1"}

Frame number: 0/63

From: /Users/TakuAndo/Documents/workspace/CambrianServer/app/controllers/battles_controller.rb @ line 17 BattlesController#index:

    12:     _BType = params[:BType]
    13:     _UId   = params[:UId]
    14:     _BTarget = params[:BTarget]
    15:     _V = params[:V]
    16: binding.pry
 => 17:     _Result = "0"
    18:     case _UId
    19:     when "Kingdom"
    20:       case _BTarget
    21:       when "Kingdom"
    22:         _BWinner = 0

[1] pry(#<BattlesController>)> params
=> {"controller"=>"battles", "action"=>"index", "BType"=>"arena", "UId"=>"Kingdom", "BTarget"=>"Pluto", "V"=>"1"}
```
```ruby:develop環境で動かした場合のparamsの中身
{"controller"=>"battles", "action"=>"index", "BType"=>"arena", "UId"=>"Kingdom", "BTarget"=>"Pluto", "V"=>"1"}
```

見比べてテストケースの動かし方が間違っておりました。

```text:誤
:
  get :index, battle: { BType: "arena", UId: "Kingdom", BTarget: "Pluto", V: "1" }
:
```
```text:正
:
  get :index,  BType: "arena", UId: "Tribal", BTarget: "Spiritual", V: "1"
:
```

修正版がこちらになります。

```rb:test/controllers/battles_controller_test.rb
require 'test_helper'

class BattlesControllerTest < ActionController::TestCase
  setup do
#    @battle = battles(:one)
#    @battle = battle
    request.env["HTTP_ACCEPT"] = 'application/json'
  end

  test "should get index" do
    get :index
    assert_response :success
    # assert_not_nil assigns(:battles)
  end

  test "should get battle won the pluto" do
    # get :index, battle: { BType: "arena", UId: "Kingdom", BTarget: "Pluto", V: "1" }
    get :index, BType: "arena", UId: "Kingdom", BTarget: "Pluto", V: "1"
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 1, res['BWinner']
  end
  test "should get battle won the Kingdom" do
    get :index, BType: "arena", UId: "Tribal", BTarget: "Kingdom", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 1, res['BWinner']
  end
  test "should get battle won the Spiritual" do
    get :index, BType: "arena", UId: "Pluto", BTarget: "Spiritual", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 1, res['BWinner']
  end
  test "should get battle won the Tribal" do
    get :index, BType: "arena", UId: "Spiritual", BTarget: "Tribal", V: "1"
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 1, res['BWinner']
  end

  test "should get battle losed the pluto" do
    get :index, BType: "arena", UId: "Spiritual", BTarget: "Pluto", V: "1"
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal -1, res['BWinner']
  end
  test "should get battle losed the Kingdom" do
    get :index, BType: "arena", UId: "Pluto", BTarget: "Kingdom", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal -1, res['BWinner']
  end
  test "should get battle losed the Spiritual" do
    get :index,  BType: "arena", UId: "Tribal", BTarget: "Spiritual", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal -1, res['BWinner']
  end
  test "should get battle losed the Tribal" do
    get :index, BType: "arena", UId: "Kingdom", BTarget: "Tribal", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal -1, res['BWinner']
  end

  test "should get battle draw the same pluto" do
    get :index, BType: "arena", UId: "Pluto", BTarget: "Pluto", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the same Kingdom" do
    get :index, BType: "arena", UId: "Kingdom", BTarget: "Kingdom", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the same Spiritual" do
    get :index, BType: "arena", UId: "Spiritual", BTarget: "Spiritual", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the same Tribal" do
    get :index, BType: "arena", UId: "Tribal", BTarget: "Tribal", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end

  test "should get battle draw the pluto" do
    get :index, BType: "arena", UId: "Tribal", BTarget: "Pluto", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the Kingdom" do
    get :index, BType: "arena", UId: "Spiritual", BTarget: "Kingdom", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the Spiritual" do
    get :index, BType: "arena", UId: "Kingdom", BTarget: "Spiritual", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the Tribal" do
    get :index, BType: "arena", UId: "Pluto", BTarget: "Tribal", V: "1" 
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0 ,  res['Result']
    assert_equal 0 , res['BWinner']
  end
end
```

```bash:テスト実行結果
$ rake test
Run options: --seed 12569

# Running:

......................

Finished in 0.249347s, 88.2303 runs/s, 228.5968 assertions/s.

22 runs, 57 assertions, 0 failures, 0 errors, 0 skips
```

今回のテストはUTのテストをパスすることを目的にやっただけですので、
ここからクラスの整理等、よりカードゲームになっていくように整備していきます。

-----
いくつか言い訳

* 不定期連載です。
* 多分途中で挫折します。
* 筆者のメモも兼ねているため過去の記事も遠慮なく随時編集いたします。
* プログラミングの開発経験はあってもゲームの開発経験はこれが初めてです。

等色々ありますが、生暖かい目で応援してくださると光栄です。