gpt4 book ai didi

javascript - 断言错误 : expected { status: 'SUCCESS' , 数据 : [] } to equal { Object (status, 数据)}

转载 作者:行者123 更新时间:2023-11-28 20:35:23 31 4
gpt4 key购买 nike

我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误

注意:数据是从 Azure 中获取的

spec.js

const accounts=require('./accounts');
const should=require('chai').should();
const chai=require('chai');
const chaiAsPromised=require('chai-as-promised');

chai.use(chaiAsPromised);
chai.should();

....

beforeEach(function()
{
mockResponse=
[
{
"AccountId": "xyz",
"AccountState": "Active"
}
]

it('Should get account from Azure API', function() {
return accounts.getActivatedAccounts(req.body.customerNumber).
should.eventually.equal(mockResponse);

});
**JavascriptFile**

function getActivatedAccounts(accounts) {
let promise = new Promise(function(resolve, reject) {
fetch(Url , { headers: config.headersAPIM})

.then(response => response.json())
.then(accounts => {
if (accounts) {
Accounts[accounts] = [];
for (account in accounts) {
let accountType = accounts[account]['type]'];
Accounts[email].push(accounts[account]);

}

let reply = {
status : "SUCCESS",
data : Accounts[accounts]
}
resolve(reply);
} else {
let reply = {
status : "SUCCESS",
data : accounts
}
resolve(reply);
}
})

.catch(err => {
console.log("Error: Could not find accounts");
console.log('Error:' + err);
let reply = {
status:"FAILURE",
err: "Error: Could not find accounts. " + err
}
resolve(reply);
})
});
return promise;
}

我无法提供调用该服务的 javascript 文件,我会在答案部分提供

最佳答案

听起来你在问关于 Chai 断言的问题。

equal使用严格相等,因此除非这两个对象实际上是同一个对象,否则它将失败。

eql使用深度相等比较,如果对象具有相同的属性和值,则通过。

这是一个简单的例子:

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);
chai.should();

const getActivatedAccounts = () =>
Promise.resolve({ status: 'SUCCESS', data: ['some', 'data'] });

it('Should get account from Azure API', function () {
return getActivatedAccounts()
.should.eventually.eql({ status: 'SUCCESS', data: ['some', 'data'] }); // Success!
});

关于javascript - 断言错误 : expected { status: 'SUCCESS' , 数据 : [] } to equal { Object (status, 数据)},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56002958/

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