【问题标题】:Supertest .expect(200) vs. res.status.should.equal(200);Supertest .expect(200) vs. res.status.should.equal(200);
【发布时间】:2016-07-26 02:06:04
【问题描述】:

这两者的目的是否相同?为什么它们都用在例如本教程https://codeforgeek.com/2015/07/unit-testing-nodejs-application-using-mocha/ 中?

编辑,看下面的代码:

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

// This agent refers to PORT where program is runninng.

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

// UNIT test begin

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

  // #1 should return home page

  it("should return home page",function(done){

    // calling home page api
    server
    .get("/")
    .expect("Content-type",/json/)
    .expect(200) // THis is HTTP response
    .end(function(err,res){
      // HTTP status should be 200
      res.status.should.equal(200);
      // Error key should be false.
      res.body.error.should.equal(false);
      done();
    });
  });

});

有没有必要

.expect(200)

res.status.should.equal(200);

?有什么区别?

【问题讨论】:

  • 欢迎来到 Stack Overflow!您能否详细说明您的问题,例如代码或其他东西,以便人们可以及早解决您的问题并为您提供帮助?谢谢!
  • 修改了我的问题,希望更清楚!

标签: mocha.js supertest


【解决方案1】:

.expect(200) 部分正在使用超测试工具来验证数据。 object.should.equal(value) 部分使用 shouldJS 进行验证。

我更喜欢在 .end() 中使用 shouldJS,因为它允许我根据需要进行一些数据操作、测试、日志记录等。

请注意以下来自:https://www.npmjs.com/package/supertest

如果您使用 .end() 方法 .expect() 失败的断言不会抛出 - 它们会将断言作为错误返回给 .end() 回调。

因此,在上面显示的示例代码中,如果 .expect("Content-type",/json/).expect(200) 失败,则 .end() 中没有任何内容可以捕获它。一个更好的例子是:

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

// This agent refers to PORT where program is runninng.

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

// UNIT test begin

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

  // #1 should return home page

  it("should return home page",function(done){

    // calling home page api
    server
      .get("/")
      .expect("Content-type",/json/)
      .expect(200) // THis is HTTP response
      .end(function(err,res){
        // NOTE: The .expect() failures are handled by err and is  
        //       properly passed to done. You may also add logging
        //       or other functionality here, as needed.
        if (err) {
            done(err);
        }

        // Error key should be false.
        res.body.error.should.equal(false);
        done();
      });
  });

});

更新以回答评论中的问题并在此处提供更漂亮的回复:

问题:然后执行.expect(200, done) 之类的操作会捕获错误吗?

答案:简短的回答是“是”。在我上面引用的同一页上,它具有以下内容:

这里有一个 mocha 的例子,注意你可以如何将 done 直接传递给 任何 .expect() 调用:

describe('GET /user', function() { 
  it('respond with json', function(done) { 
    request(app) 
      .get('/user') 
      .set('Accept', 'application/json') 
      .expect('Content-Type', /json/) 
      .expect(200, done); 
  }); 
});

【讨论】:

  • 然后会做类似expect(200, done) 的操作来捕获错误吗?
  • 简短的回答是“是”。在我上面引用的同一页上,它具有以下内容:Here's an example with mocha, note how you can pass done straight to any of the .expect() calls: describe('GET /user', function() { it('respond with json', function(done) { request(app) .get('/user') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); }); });
【解决方案2】:

从技术上讲没有区别,我认为你应该坚持.expect(200),就像超测示例所建议的那样:https://github.com/visionmedia/supertest

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 2017-06-03
    • 2018-12-11
    • 1970-01-01
    相关资源
    最近更新 更多