简体   繁体   English

Mocha js Uncaught TypeError:无法读取未定义的属性(读取“应该”)

[英]Mocha js Uncaught TypeError: Cannot read properties of undefined (reading 'should')

So I'm trying to test my API and with mocha js and I'm getting this error.所以我正在尝试测试我的 API 和 mocha js,我收到了这个错误。

SAMPLE unit test
1) should return posts with tag tech sorted by likes


0 passing (26ms)
1 failing

1) SAMPLE unit test
  should return home page:
  Uncaught TypeError: Cannot read properties of undefined (reading 'should')
  at Test.<anonymous> (test\get_info.test.js:22:18)
  at Test.assert (node_modules\supertest\lib\test.js:172:8)
  at localAssert (node_modules\supertest\lib\test.js:120:14)
  at C:\Users\tiktk\Desktop\Fetch\node_modules\supertest\lib\test.js:125:7
  at Test.Request.callback (node_modules\superagent\lib\node\index.js:927:3)
  at ClientRequest.<anonymous> (node_modules\superagent\lib\node\index.js:844:12)
  at ClientRequest.emit (node:events:390:28)
  at Socket.socketErrorListener (node:_http_client:447:9)
  at Socket.emit (node:events:390:28)
  at emitErrorNT (node:internal/streams/destroy:157:8)
  at emitErrorCloseNT (node:internal/streams/destroy:122:3)
  at processTicksAndRejections (node:internal/process/task_queues:83:21)

I have all the dependencies installed and up to date but I still get that error.我已经安装了所有依赖项并且是最新的,但我仍然收到该错误。

var supertest = require("supertest");
var should = require("should");

var server = supertest.agent("http://127.0.0.1:3000");

describe("Api unit test",function(){

  it("should return posts with tag tech sorted by likes",function(done){

    server
    .get("/api/posts?tags=tech&sortBy=likes&direction=desc")
    .expect("Content-type",/json/)
    .expect(200) 
    .end(function(res){
      // HTTP status should be 200
      res.status.should.equal(200);
      // Error key should be false.
      res.body.error.should.equal(false);
      done();
    });
  });
});

The link should return a bunch of objects in an array as a JSON file sorted by likes (in this case)该链接应返回数组中的一堆对象,作为按喜欢排序的 JSON 文件(在本例中)

The error most probably comes from the fact that res.body.error is undefined when you don't have an error.该错误很可能是由于当您没有错误时res.body.error是未定义的。 In which case, your test should be something like:在这种情况下,您的测试应该类似于:

res.body.should.not.have.property('error')

To confirm my hypothesis, you should console.log the content of res.body before checking for the error.为了证实我的假设,您应该在检查错误之前对res.body的内容进行 console.log。

暂无
暂无

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

相关问题 未捕获的类型错误:无法读取未定义的属性(读取“公司名称”)JS 对象 - Uncaught TypeError: Cannot read properties of undefined (reading 'companyName') JS Objects 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') Uncaught TypeError TypeError:无法读取未定义的属性(读取“路径”) - Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path') 未捕获的类型错误:无法读取未定义的属性(读取“目标”)和(读取“值”) - Uncaught TypeError: Cannot read properties of undefined (reading 'target') & (reading 'value') 未捕获的类型错误:无法读取未定义的属性(读取“未定义”) - Uncaught TypeError: Cannot read properties of undefined (reading 'undefined') Redux 工具包:未捕获的类型错误:无法读取未定义的属性(读取“类型”) - Redux toolkit: Uncaught TypeError: Cannot read properties of undefined (reading 'type')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM