0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

includeマッチャー

Posted at

オブジェクトがinclude?メソッドに対応していれば、動作する 含むかどうか
includeマッチャー

include matcher

コレクションが期待したオブジェクトを一つ以上含んでいることを明示するためにマッチャーを使って下さい。指定されたコレクション内のいくつかのオブジェクトが明示されたマッチャーを通った場合成功する。
これはincloud?(文字列または配列のような)に応答するオブジェクトに対して動作する

Use the include matcher to specify that a collection includes one or more expected objects. It succeeds if any object of the given collection passes the specified matcher. This works on any object that responds to #include? (such as a string or array):

 RSpec.describe "test" do
  it "include" do
    expect("a string").to include("a")
    expect("a string").to include(/a|str/).twice # 正規表現も使える
    expect("a string").to include("str", "g")
    expect("a string").not_to include("foo")
  end
end
test
  include 

Finished in 0.03171 seconds (files took 1.83 seconds to load)
1 example, 0 failures
  expect([1, 2]).to include(1)
  expect([1, 2]).to include(1, 2)
  expect([1, 2]).to include(a_kind_of(Integer)) # 要素のクラスを調べてるらしい
  expect([1, 2]).to include(be_odd.and be < 10 )
  expect([1, 2]).to include(be_odd)
  expect([1, 2]).to include(be < 10).at_least(2).times
  expect([1, 2]).not_to include(17) # 含まれていないか?
test
  include

Finished in 0.03378 seconds (files took 1.85 seconds to load)
1 example, 0 failures

マッチャーはハッシュに対して柔軟な処理も供給する
The matcher also provides flexible handling for hashes:

  expect(:a => 1, :b => 2).to include(:a)
  expect(:a => 1, :b => 2).to include(:a, :b)
  expect(:a => 1, :b => 2).to include(:a => 1)
  expect(:a => 1, :b => 2).to include(:b => 2, :a => 1)
  expect(:a => 1, :b => 2).to include(match(/b/) => 2)
  expect(:a => 1, :b => 2).to include(match(/b/) => be_even)
  expect(:a => 1, :b => 2).not_to include(:c)
  expect(:a => 1, :b => 2).not_to include(:a => 2)
  expect(:a => 1, :b => 2).not_to include(:c => 3)
test
  include 

Finished in 0.03866 seconds (files took 1.45 seconds to load)
1 example, 0 failures

開いたページにdivタグが含まれていることもわかる

expect(response.body).to include("<div>")

include?

not_to

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?