【问题标题】:Testing Node app with Jasmine for status: 200使用 Jasmine 测试节点应用程序的状态:200
【发布时间】:2015-11-02 00:18:59
【问题描述】:

我正在为使用 Node.js 和 Express 构建的应用程序编写 Jasmine 测试。第一个测试是查看应用程序是否响应 statusCode 200。我正在通过this tutorial 工作,它向您展示了如何做到这一点,但我遇到了障碍。

jasmine-node 不会运行测试。没有任何失败;它只是不提供报告。显然,this is a known bug.

看看jasmine-node project,不过一年多没更新了!查看主要的 jasmine 项目,我看到 it now has support for Node! 那么,是否已经放弃了 jasmine-node 以支持在 jasmine 中添加对 Node 的支持?在安装jasmine 并按照规范运行它之后,我现在遇到了一个新问题。当运行jasminenpm test 时,我得到的不是 Jasmine 测试失败,而是

Started

TypeError: Cannot read property 'statusCode' of undefined

这是我的规范文件:

var request = require("request");
var base_url = "http://localhost:3000/";

describe("Hello World Server", function() {
  describe("GET /", function() {
    it("returns status code 200", function(done) {
      request.get(base_url, function(error, response, body) {
        expect(response.statusCode).toBe(200);
        done();
      });
    });

    it("returns Hello World", function(done) {
      request.get(base_url, function(error, response, body) {
        expect(body).toBe("Hello World");
        done();
      });
    });
  });
});

【问题讨论】:

    标签: javascript node.js jasmine jasmine-node


    【解决方案1】:

    您似乎没有得到有效的回复。您应该在回调函数中查看error

    request.get(base_url, function(error, response, body) {
    
        // Check for error
        if(error){
            console.log(error);
            // Probably assert a failure here.
        }
    
        expect(response.statusCode).toBe(200);
        done();
    });
    

    在使用console.log 检查错误后,您可能不想删除日志记录并将其替换为一些 jasmine 逻辑以进行错误检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 2012-08-06
      • 2013-12-16
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多