gpt4 book ai didi

javascript - 使用 json 模式进行 mocha 全覆盖测试

转载 作者:行者123 更新时间:2023-12-03 10:44:07 24 4
gpt4 key购买 nike

我已经为 get 请求编写了一个测试,但希望获得此测试的“完全覆盖”。基本上,我想使用 JSON Schema 之类的东西来验证 JSON 返回是否符合我的期望。

到目前为止我的代码如下:

var winston = require('winston');
//var chai = require('chai');

var request = require('supertest-as-promised');
var testUtils = require('./utils/test_utils');

var API_ROOT = 'http://localhost:8000/mywebsite';

var agent = request(API_ROOT);

describe('/my-profile/', function(){
describe('GET', function() {

var url = '/my-profile/';

it('should return valid payload', function(done) {

agent.
get(url).
set('Content-Type', 'application/json').
expect('Content-Type', /json/).
expect(200).
// then(testUtils.logResponse).
then(function(res) {
done();
}).catch(function(err) {
// winston.log(res).then(done);
done(err);
});

});
});


});

如何使用 json 模式进行验证。有样本链接吗?评论?代码?

最佳答案

您可以尝试使用 Chai json-schema plugin 。这让您可以做出如下断言:


期望(goodApple).to.be.jsonSchema(fruitSchema);
期望(badApple).to.not.be.jsonSchema(fruitSchema)

请访问网站了解完整详细信息。

关于javascript - 使用 json 模式进行 mocha 全覆盖测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28631959/

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