简体   繁体   English

比较开玩笑单元测试用例中的响应

[英]Compare response in jest unit test case

I'm new to jest unit test case scenario, I have a scenario where in the response from the service that I called is of the below format我是 jest 单元测试用例场景的新手,我有一个场景,在我调用的服务的响应中,我调用的格式如下

Artifact {
        name: 'detection-v1.zip',
        file_path: 'artifact\\bn-ds-anomalydetection-v1.zip',
        is_tenant: false,
        metadata: [
          Registerfact {
            name: 'ad',
            _meta: [Object],
            line_meta: [Object]
          },
          Registerfact {
            name: 'ad-generic',
            _meta: [Object],
            line_meta: [Object]
          }
        ]
      }

how can i compare the above response in the jest service, I was trying to create a object but the Artifact name before the object is confusing how should i proceed我如何比较开玩笑服务中的上述响应,我试图创建一个 object 但 object 之前的工件名称令人困惑我应该如何继续

The test case is测试用例是

test('test processArtifact method', async()=>{
    const mockGetRestClient = jest.fn();
    
    try{
      const response = await factService.processfact(artifact)
      console.log("response---",response)
      // expect(response).toEqual()
    }
    catch(e){ }
  })

I know its silly question,but i'm confused hence posted it.我知道它的愚蠢问题,但我很困惑,因此发布了它。

How should i create the static object to be put in.toEqual()?我应该如何创建 static object 以放入.toEqual()?

You can declare a global/static var with your response object on top of file.您可以在文件顶部使用响应 object 声明全局/静态变量。 Or better declare it in some constants file and import here.或者最好在一些常量文件中声明它并在此处导入。

For Comparison: Usually, if you have a simple object, you can use JSON.stringify .比较:通常,如果你有一个简单的 object,你可以使用JSON.stringify However, it may give error due to different order of object keys.但是,由于 object 键的顺序不同,它可能会出错。

You should use assert for the deep comparison.您应该使用assert进行深度比较。 There is method assert.deepEqual() which does deep comparison of objects.有方法assert.deepEqual()可以对对象进行深度比较。

an example for using assert from official docs使用官方文档中的断言的示例

import assert from 'node:assert';

const obj1 = {
  a: {
    b: 1
  }
};
const obj2 = {
  a: {
    b: 2
  }
};
const obj3 = {
  a: {
    b: 1
  }
};
const obj4 = Object.create(obj1);

assert.deepEqual(obj1, obj1);
// OK

// Values of b are different:
assert.deepEqual(obj1, obj2);
// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }

assert.deepEqual(obj1, obj3);
// OK

// Prototypes are ignored:
assert.deepEqual(obj1, obj4);
// AssertionError: { a: { b: 1 } } deepEqual {}

Hope this helps, let me know if you have any questions.希望这会有所帮助,如果您有任何问题,请告诉我。

You can use JSON.stringify in order to convert your object into a string and then compare this result to the one you expect.您可以使用JSON.stringify将 object 转换为字符串,然后将此结果与您期望的结果进行比较。

 console.log(JSON.stringify({ name: 'detection-v1.zip', file_path: 'artifact\\bn-ds-anomalydetection-v1.zip', is_tenant: false, metadata: [ {Registerfact: { name: 'ad', _meta: {}, line_meta: {} }}, {Registerfact: { name: 'ad-generic', _meta: {}, line_meta: {} }} ] }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM