3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Frisby.jsのTips(Custom Matcherの使い方)

Posted at

REST APIのテストフレームワークであるFrisby.jsのTips。

sample.json
{
    "member": [
        {
            "name": "tomute",
            "hobby": "ski"
        },
        {
            "name": "tom",
            "hobby": null
        },
        {
            "name": "kate",
            "hobby": "football"
        }
    ]
}

上記のようにnullが含まれるようなJSONが返ってくるAPIを、以下のようにexpectJSONTypes()でStringとして検証すると、「Error: Expected 'null' to be type 'string' on key 'hobby'」というようなエラーになってしまう。

test_spec.js
frisby.create('Test using a path as the paramater')
    .get('http://localhost:3000/test')
    .expectJSONTypes('member.*', {
        "name": String,
        "hobby": String
    })
    .toss()

このような場合にはFrisby.jsが用意するCustom Matcherを利用して、以下のような検証コードにすれば良い。

test_spec.js
frisby.create('Test using a path as the paramater')
    .get('http://localhost:3000/test')
    .expectJSONTypes('member.*', {
        "name": String,
        "hobby": function(val) { expect(val).toBeTypeOrNull(String); }
    })
    .toss()

Frisby.jsが用意するCustom Matcherには他にもtoMatchOrBeNull、toMatchOrBeEmpty、toBeTypeOrUndefined等がある。
また自身でCustom Matcherを作成する事も可能である。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?