LoginSignup
5
3

More than 5 years have passed since last update.

Crystal のテスト(Spec)の機能一覧(2016/06/21時点)

Posted at

Crystal のテスト(Spec) の機能を一通り確認します。

Structure

describe / context / it

require "spec"

class Person
  getter name : String
  getter age : Int32
  property friends : Array(Person)

  def initialize(@name, @age)
  end
end

describe Person do
  context "context1" do
    it "it1" do
      true.should be_true
      true.should eq(true)
    end
  end
end
  • 実行結果
.

Finished in 0.58 milliseconds
1 examples, 0 failures, 0 errors, 0 pending

pending

保留する

require "spec"

class Person
  getter name : String
  getter age : Int32
  property friends : Array(Person)

  def initialize(@name, @age)
    @friends = [] of Person
  end
end

describe Person do
  context "context1" do
    pending "pending" do
      true.should be_true
      true.should eq(true)
    end
  end
end
  • 実行結果
*

Pending:
  Person context1 pending

Finished in 0.63 milliseconds
1 examples, 0 failures, 0 errors, 1 pending

fail

意図的に例外を発生させる

require "spec"

class Person
  getter name : String
  getter age : Int32
  property friends : Array(Person)

  def initialize(@name, @age)
    @friends = [] of Person
  end
end

describe Person do
  it "it1" do
    fail "fail readson"
  end
end
  • 実行結果
F

Failures:

  1) Person it1
     Failure/Error: fail "fail readson"

       fail readson

     # /path/to/fail.cr:15

Finished in 0.57 milliseconds
1 examples, 1 failures, 0 errors, 0 pending

Failed examples:

crystal spec /path/to/fail.cr:14 # Person it1
[Finished in 0.695s]

Expectation

be - Spec::Be

Crystal 本体のコード

Spec::Be - crystal-lang/crystal - GitHub

説明

比較演算( >, >=, <, <= )用の Expectation

サンプルコード

require "spec"

describe "be" do
  it "範囲 1..3 の整数の乱数は1以上3以下" do
    actual = rand(1..3)
    actual.should be >= 1
    actual.should be <= 3
  end

  it "範囲 1...3 の整数の乱数は1以上3未満" do
    actual = rand(1...3)
    actual.should be >= 1
    actual.should be < 3
  end

  it "a..z の後半分の中から任意の1要素を抽出すると n より大きい文字コードである" do
    list = ('a'..'z').to_a
    actual = list[(list.size / 2)..-1].sample
    actual.should be > 'n'
  end
end

be(value) - Spec::BeExpectation

Crystal 本体のコード

Spec::BeExpectation - crystal-lang/crystal - GitHub

説明

実行結果と期待値を same? メソッドで比較した結果で判定します。

same?object_id による比較=同一性の比較を行います。

サンプルコード

require "spec"

describe "be value" do
  it "同値かつ同一のケース" do
    a = [1, 2, 3]
    a.should be(a)
  end

  it "同値だが同一ではないケース" do
    [1, 2, 3].should_not be([1, 2, 3])
  end
end

be_close(expected, delta) - Spec::CloseExpectation

Crystal 本体のコード

Spec::CloseExpectation - crystal-lang/crystal - GitHub

説明

浮動小数点の演算など、誤差を加味した比較を行います。

サンプルコード

require "spec"

describe "be close" do
  it "浮動小数点の誤差を加味した比較" do
    (1 / 3.0).should be_close(0.33, 0.1)
  end
end

be_false

Crystal 本体のコード

expectations - crystal-lang/crystal - GitHub

説明

falseかどうか判定します。

サンプルコード

require "spec"

describe "be false" do
  it "false" do
    false.should be_false
  end

  it "true" do
    true.should_not be_false
  end
end

be_falsey - Spec::BeFalseyExpectation

Crystal 本体のコード

Spec::BeFalseyExpectation - crystal-lang/crystal - GitHub

説明

falseyかどうか判定します。

サンプルコード

require "spec"

describe "be falsey" do
  it "falsey" do
    false.should be_falsey
    nil.should be_falsey
  end

  it "truthy" do
    true.should_not be_false
    'a'.should_not be_false
  end
end

be_nil

Crystal 本体のコード

expectations - crystal-lang/crystal - GitHub

説明

nilかどうか判定します。

サンプルコード

require "spec"

describe "be nil" do
  it "nil" do
    nil.should be_nil
  end

  it "not nil" do
    false.should_not be_nil
    true.should_not be_nil
  end
end

be_true

Crystal 本体のコード

expectations - crystal-lang/crystal - GitHub

説明

trueかどうか判定します。

サンプルコード

require "spec"

describe "be true" do
  it "true" do
    true.should be_true
  end

  it "false" do
    false.should_not be_true
  end
end

be_truthy - Spec::BeTruthyExpectation

Crystal 本体のコード

Spec::BeTruthyExpectation - crystal-lang/crystal - GitHub

説明

truthyかどうか判定します。

サンプルコード

require "spec"

describe "be truthy" do
  it "truthy" do
    true.should be_truthy
    'a'.should be_truthy
  end

  it "falsey" do
    false.should_not be_truthy
    nil.should_not be_truthy
  end
end

contain(expected) - Spec::ContainExpectation

Crystal 本体のコード

Spec::ContainExpectation - crystal-lang/crystal - GitHub

説明

実行結果が期待値を含んでいるかどうか判定します。

内部実装としては include? で判定しています。

サンプルコード

require "spec"

describe "contain" do
  it "include" do
    [1, 2, 3].should contain(1)
    [1, 2, 3].should contain(3)
  end

  it "not include" do
    [1, 2, 3].should_not contain(0)
    [1, 2, 3].should_not contain(4)
  end
end

eq(value) - Spec::EqualExpectation

Crystal 本体のコード

Spec::EqualExpectation - crystal-lang/crystal - GitHub

説明

実行結果と期待値を == メソッドで比較した結果で判定します。

== は同値性の比較を行います。

サンプルコード

require "spec"

describe "eq value" do
  it "同値かつ同一のケース" do
    a = [1, 2, 3]
    a.should eq(a)
  end

  it "同値だが同一ではないケース" do
    [1, 2, 3].should eq([1, 2, 3])
  end
end

match(value) - Spec::MatchExpectation

Crystal 本体のコード

Spec::MatchExpectation - crystal-lang/crystal - GitHub

説明

実行結果が期待される正規表現にマッチするか判定します。

サンプルコード

require "spec"

describe "match" do
  it "match" do
    %w(hoge hige hage).sample.should match(/h.ge/)
  end

  it "not match" do
    %w(hoge hige hage).sample.should_not match(/h.ello/)
  end
end

外部資料

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3