gpt4 book ai didi

javascript - 使用 Jasmine 测试 Node 应用程序的状态 : 200

转载 作者:太空宇宙 更新时间:2023-11-04 02:20:51 25 4
gpt4 key购买 nike

我正在为使用 Node.js 和 Express 构建的应用程序编写 Jasmine 测试。第一个测试是查看应用程序是否以 200 的 statusCode 进行响应。我正在处理 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();
});
});
});
});

最佳答案

您似乎没有收到有效的回复。您应该检查回调函数中的错误:

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 逻辑。

关于javascript - 使用 Jasmine 测试 Node 应用程序的状态 : 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33468854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com