English 中文(简体)
Express Script Super specific specific specific specific specific specific
原标题:Express JavaScript Supertest Expect specific json fields

I m试图为APIC末点撰写一些单位测试,并在Javadspew/超标上决定。 我走了基本坡道,但处理具体问题,以检查应对措施。 我想把尸体捆绑起来,检查预期的外地回报是否正确。 我在网上看到的多数东西都使用这一方法,但在我尝试时总是通过这种方法,即便在我进入价值观时,我知道在初生中还不存在。 任何建议? 这里是我的法典:

describe( GET category , function () {
    it( response w/ only injury returned , function () {
        request( endpoint )
            .get( path )
            .set( header ,  token )
            .expect(200)
            .then(response => {
                console.assert(response.body, "Baseball")
            })
    })
});

I have also tried changing the .then to .expect, with same results. If I do response.body.specificFieldinBody I get similar results. Any help?

问题回答

你可以这样做——使用<代码>then syntax,但我想使用Supertest assertion syntax。

This mean, use response.body.should.have.property("Baseball"); instead of console.assert(response.body, "Baseball") (Baseball is your special field).

或 我的建议是制定重新使用守则: 下面的<代码>expects节规定追索功能。

const isIncludeField = function (fieldName) {
  return function (res) {
    res.body.should.have.property(fieldName);
  };
}

describe( GET category , function () {
  it( response w/ only injury returned , function () {
    request( endpoint )
      .get( path )
      .set( header ,  token )
      .expect(200)
      .expect(isIncludeField( Baseball )) // 
      .end(done); // call done callback
  })
});

我看到三个问题:

  1. <代码>console.assert(response.one, “Baseball”, 仅检查response. .one 是真理。 <代码>Baseball>只是以下信息:如印有t(即Assertion : Baseball)。 你也许会期待

    console.assert(response.body.Baseball, "body.Baseball should be defined")
    

    或您可能希望指出,某一关键数值等于>“Baseball”;例如,

    console.assert(response.body.injury === "Baseball", "body.injury ===  Baseball ")
    
  2. www.un.org/Depts/DGACM/index_french.htm 仅记录未果,可能won t becred/a>,由任何测试图书馆接收(Mocha, Jest)。 我建议使用诸如Chai、Jest的建筑主张或至少是Node s本地的asse等断言图书馆,因此测试将显示失败。

  3. <代码>request(终点) 退回允诺。 如果测试套是用来评估试样的超常性质,那么,它会给你一个即时的通行证,而实际上却没有任何说法。 使用<代码>return request ( endpoint ),以确保测试套期等待承诺链解决和发现任何说法。

Marking your test async helps reduce the footgun factor and is generally cleaner than .then() or callbacks (less nesting). Here s a complete example you can run with npx mocha server.test.js.

server.test.js:

const assert = require("assert");
const request = require("supertest");
const app = require("./server");

describe("GET category", () => {
  it("response with only injury returned", async () => {
    const response = await request(app).get("/path");
    assert(response.status === 200);
    assert(response.body.injury === "Baseball");
  });

  // ... or use supertest s assertions:
  it("response with only injury returned (alternate approach)", () => {
    return request(app)
      .get("/path")
      .expect(200, {injury: "Baseball"});
  });
});

<编码>server.js:

const express = require("express");
const app = express();

app.get("/path", (req, res) => res.json({injury: "Baseball"}));

module.exports = app;

www.un.org/Depts/DGACM/index_french.htm

{
  "dependencies": {
    "express": "^4.18.3"
  },
  "devDependencies": {
    "mocha": "^10.3.0",
    "supertest": "^6.3.4"
  }
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签