私はまさに、このようなタスクのためのNode.jsモジュールを作成しました:検証期待オブジェクトで提供されているフィールドだけを。
以下の例では、actual
(テストで返されました)とexpected
(期待される結果)の2つのオブジェクトを比較したいと思います。 actual
のフィールドの中には自動生成されたフィールド(_id
)があるので、それらが存在し、特別なフォーマットを持っていることを確認する必要があります。私が比較したくないフィールドのいくつか(タイプwork
の電話、account.number
)。
// Some test method returns this object
let actual = {
_id: '5945bf36ccb3fa0011e8533c',
name: 'John',
email: '[email protected]',
phones: [
{ type: 'mobile', number: '1234567' }
{ type: 'work', number: '567382' }
],
account: {
number: '11111',
registered: '2010-04-21'
},
createdAt: '2017-02-03'
};
// Ned to do the following assertions:
// _id is exists
// name and email match
// mobile phone is exists and it's a number
// account registered match
let expected = {
_id: '_mock_',
name: 'John',
email: '[email protected]',
phones: [
{ type: 'mobile', number: /^d{1,10}$/ }
],
account: {
registered: '2010-04-21'
}
}
nassert.assert(actual, expected); // should pass
リンク::だからexpected
オブジェクトは、テストのために必要なだけのフィールドが含ま可能な解決策のn-assert
つをすべて 'expected'オブジェクトフィールドのリストを取得し、すべてのactual'オブジェクト'から削除することです'expectedFields'が必要です。その後、それらを主張する。 – alexmac
@alexmacいいですね。 – musicliftsme