rubyでドローポーカーを作ってみる～test-unit準備編～


## 概要

[rubyでドローポーカーを作ってみる～準備編～](https://qiita.com/r12tkmt/items/56a60f2d69226b2bdcf2)

に続いて。

ソース: https://github.com/rytkmt/ruby_poker

test-unit全然知らない・・・ということで今回はtest-unitの準備をしていきます。

## 準備

こちらの[公式ページ](https://test-unit.github.io/test-unit/ja/file.getting-started.html#test-unit)を参考に進めていく

が、gemではないので少し手順が変わりそう・・・
なのでそれも含めて手順を記録として残していきます

### 初期ファイルの準備

```
$ cd ruby_poker
$ bundle gem -t minitest ./
```

```
git status -sb

A  .gitignore
A  .travis.yml
A  CODE_OF_CONDUCT.md
A  Gemfile
A  LICENSE.txt
A  Rakefile
A  bin/console
A  bin/setup
A  lib/ruby_poker.rb
A  lib/ruby_poker/version.rb
A  ruby_poker.gemspec
A  test/ruby_poker_test.rb
A  test/test_helper.rb
```

結構作られる

gemではないためgemspec削除

```
$ git rm ruby_poker.gemspec
```

### ファイルの修正

#### Gemfile

```diff:Gemfile
-# Specify your gem's dependencies in ruby_poker.gemspec
-gemspec
+gem "rake"
+group :test do
+  gem "pry-byebug"
+  gem "test-unit"
+  gem "test-unit-rr"
+end
```

gemspecの行削除
代わりにminitestからtest-unitに変更なのでまずgemをインストール

```shell
$ bundle install
```

このとき、bundlerにてインストールされたファイルがgitに上がらないように`.gitignore`を必要に応じて編集

#### Rakefile

```diff:Rakefile
-require "bundler/gem_tasks"
 require "rake/testtask"
```

#### test/test_helper.rb

```diff:test/test_helper.rb
 $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
 require "ruby_poker"
 
-require "minitest/autorun"
+require "pry"
+require "test/unit"
+require "test/unit/rr"
```

#### test/ruby_poker_test.rb

```diff:test/ruby_poker_test.rb
 require "test_helper"
 
-class RubyPokerTest < Minitest::Test
+class RubyPokerTest < Test::Unit::TestCase
   def test_that_it_has_a_version_number
     refute_nil ::RubyPoker::VERSION
   end
```

#### bin/console

`bundle exec console`を実行したときにruby_pokerはLOAD_PATHに居ないためエラーになる。そのためLOAD_PATHに追加する

```diff:bin/console
 require "bundler/setup"
+$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
+
 require "ruby_poker"
```

### 動作確認

#### console

`bundle exec console` を実行し、irbが立ち上がればOK

#### test

`bundle exec rake test`を実行し1件は失敗するようにテストケースがサンプルとして作られてるため、1件成功、1件失敗となればOK

```shell
$ rake test
/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/file_utils.rb:54: warning: Insecure world writable dir /home in PATH, mode 040707
Loaded suite /home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/rake_test_loader
Started
F
==============================================================================================================================================================
      6:   end
      7: 
      8:   def test_it_does_something_useful
  =>  9:     assert false
     10:   end
     11: end
/home/vagrant/private_workspace/ruby_poker/test/ruby_poker_test.rb:9:in `test_it_does_something_useful'
Failure: test_it_does_something_useful(RubyPokerTest): <false> is not true.
==============================================================================================================================================================
.
Finished in 0.004560792 seconds.
--------------------------------------------------------------------------------------------------------------------------------------------------------------
2 tests, 2 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
50% passed
--------------------------------------------------------------------------------------------------------------------------------------------------------------
438.52 tests/s, 438.52 assertions/s
rake aborted!
Command failed with status (1)

Tasks: TOP => test
(See full trace by running task with --trace)
```

## 続き

↓
[rubyでドローポーカーを作ってみる～実装編1（カード）～](https://qiita.com/r12tkmt/items/175aa66fd91995f13524)