- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我尝试使用 sinon、chai、mocha 进行测试的一段 node.js 代码。然而我不明白为什么我无法在 sinon 的拒绝中传递参数。我尝试寻找在线帮助和文档,但仍然无法找到适当的原因。这是我正在尝试测试的代码:
this.retrieveSomething = function () {
var promiseFunc = function (resolve, reject) {
Repository.findSomething( {$or: [{"status":"x"},{"status":"y"}]}, 'name description status')
.then(function (result) {
resolve(result);
})
.catch(function (err) {
reject(new errors.InternalServerError('Failed to find Surveys',
{errors: [{message: 'Failed '}, {details: err.errors}]}));
});
};
return new Promise(promiseFunc);
};
这里是测试代码
it('failure', function (done) {
var findSomethingStub = sinon.stub(Repository, 'findSomething');
findSomethingStub.returnsPromise().rejects();
var promise = fixture.retrieveSurveysVast();
setTimeout(function () {
expect(findSomethingStub.calledOnce).to.be.true;
expect(promise).to.be.eventually.deep.equal("failed");
Repository.findSomething.restore();
done();
}, 5);
});
此案顺利通过。然而,如果我尝试以这种方式拒绝它,这会表现得很奇怪
findSomethingStub.returnsPromise().rejects("failed");
然后像这样匹配
expect(promise).to.be.eventually.deep.equal("failed");
它说
Unhandled rejection InternalServerError: Failed to find Surveys
事实上,我在 equal 中给出的内容并不重要。请帮助解释为什么我无法传递参数来拒绝并期望它等于相同的参数。
最佳答案
希望我的回答对您有用。查看您的测试脚本.. 首先,您对 Repository.findSomething
进行 stub 以返回带有字符串“failed”的被拒绝的 promise 。因此,在实际的this.retrieveSomething
代码中,会落入catch语句中:
.catch(function (err) {
reject(new errors.InternalServerError('Failed to find Surveys',
{errors: [{message: 'Failed '}, {details: err.errors}]}));
});
这意味着, (err) 将包含字符串“failed”,作为您 stub 的拒绝 promise 的结果。之后,this.retrieveSomething
将返回 Promise Reject,但它的值将由函数 error.InternalServerError
处理,该函数采用上面的 2 个参数。因此,函数 this.retrieveSomething
的拒绝值取决于您的 error.InternalServerError
实现。
还有一件事要注意,我认为你被拒绝的 stub findSomethingStub.returnsPromise().rejects("failed");
不会产生任何效果,因为 error.InternalServerError 会抓取 err.errors
所以至少应该是这样的: findSomethingStub.returnsPromise().rejects({errors: "failed"});
我尝试通过假设该错误来模拟您的代码。InternalServerError 返回如下字符串:
class Errors {
InternalServerError(string, obj) {
return `error: ${string}, message: ${obj.errors[0].message}, details: ${obj.errors[1].details}`;
} }
然后,在测试用例中我尝试
const fixture = require('../47573532-sinon-react/Fixture');
const Repository = require('../47573532-sinon-react/Repository');
const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const chaiAsPromised = require('chai-as-promised');
const sinonStubPromise = require('sinon-stub-promise');
sinonStubPromise(sinon);
chai.use(chaiAsPromised);
chai.use(sinonChai);
chai.should();
describe('/47573532-sinon-react', () => {
it('failure (using sinon, chai, sinon-stub-promise, chai-as-promised)', (done) => {
const findSomethingStub = sinon.stub(Repository, 'findSomething');
findSomethingStub.returnsPromise().rejects({ errors: 'failed' });
const promise = fixture.retrieveSurveysVast();
promise.should.be.rejected.then((err) => {
err.should.be.deep.equal
(
'error: Failed to find Surveys, message: Failed , details: failed'
);
findSomethingStub.restore();
}).should.notify(done);
});
});
首先,它会断言promise.should.be.rejected,然后根据InternalServerError实现结果评估错误。在这种情况下,我们需要确保 details: 'failed'
与 stub 拒绝的内容匹配。如果我们用其他内容更改 details
值,测试将会失败。
it('failure (using sinon, chai, sinon-stub-promise, chai-as-promised)', (done) => {
const findSomethingStub = sinon.stub(Repository, 'findSomething');
findSomethingStub.returnsPromise().rejects({ errors: 'failed' });
const promise = fixture.retrieveSurveysVast();
promise.should.be.rejected.then((err) => {
err.should.be.deep.equal
(
'error: Failed to find Surveys, message: Failed , details: something not from stub'
); // this will cause test failed
findSomethingStub.restore();
}).should.notify(done);
});
关于node.js - 无法在 sinon 测试用例中传递拒绝内的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47573532/
我只想允许一个国家/地区访问,但排除该国家/地区内的代理。 这就是我所拥有的(为了方便起见,缩短了版本) order deny,allow deny from all allow from 139.
这个问题在这里已经有了答案: What is an unhandled promise rejection? (9 个回答) 关闭 4 年前。 我目前正在尝试实现我自己的 Promise,以便在 A
我在使用 Gitolite 推送 git 时遇到问题。 当我尝试这个时: git push origin :refs/tags/deployment 我收到这个错误: remote: D NAME/i
我已经为我的 laravel 5.0-dev 项目配置了 mysql,如下所示: 'mysql' => [ 'driver' => 'mysql', 'host' =>
我对 Web 和 SOF 进行了一些研究,但发现对于该错误没有任何真正的帮助。 我使用 Windows 10 Ubuntu Bash 安装了 Node 和 Puppeteer,但未能使其工作,但我设法
在我的应用审核期间,我收到了以下信息: “17.2:要求用户共享个人信息(例如电子邮件地址和生日)才能正常运行的应用将被拒绝 具体来说,您的应用仅使用Facebook登录名进行身份验证,但不包括该网站
我正在开发 VeriFone VX 终端的接口(interface)。虽然,这确实是一个普遍的 EMV 问题。我们的处理器的下限为零,因此它将始终在线发送。但是,如果它发生变化,您如何知道(哪些标签)
我编写了一些宏代码,根据表单提交向经理发送电子邮件(用于费用/审批流程),这是我使用谷歌表单/电子表格的第一个项目,所以也许我可能会错过一些简单的东西,但我为此浏览了 2 个教程,我的代码与重要的部分
clang 3.4 接受以下代码;而 vc++ NOV 2013 CTP 拒绝它并出现错误: error C2668: 'AreEqual' : ambiguous call to overloade
使用 nginx,您可以允许和拒绝范围和 ips (https://www.nginx.com/resources/admin-guide/restricting-access/)。使用realip模
官方编辑: 非常感谢您的帮助,但我仍然遇到问题。 我的 ffserver.conf 文件是这样的: # Port on which the server is listening. You must
我有一个问题:我是 Ubuntu 系统的根。我想授予用户(比如用户名是 X)执行任何命令的权限,但同时我有一个文件夹,除了我的用户(当然不是 X,因为它是 Admin ) 或根。有什么建议么?谢谢!
我使用 Apache2.2 作为 tomcat 服务器的前端。我想限制对某个位置的访问,但允许对子位置的所有访问,但遇到了一些麻烦。 我目前拥有的是: AllowOverride None
就像 this person ,我一直在为浏览器缓存 SSL session 而苦苦挣扎。简而言之,如果选择了客户端证书,则无法以编程方式清除状态,除非在 IE 中使用 document.execCo
我的网站是在由 Apache 服务器提供服务的 Angular 上设置的。我通过 View 将内容动态加载到主页上。 现在以下是我的问题: 我建立这个网站的主要目的是通过 google adsense
我最近遇到了我的应用程序的问题,当它突然被 Google Play 拒绝时因为他们发现我使用的是背景位置 .但实际上我并没有使用这个功能。我只有 ACCESS_COARSE_LOCATION和 ACC
function sendPushNotification(subscription, urlEncodedData){ try { webpush.sendNotification(su
我包裹了一个 request-promise-native调用返回 promise 的函数。 import request from 'request-promise-native'; functio
我正在开发我的 meteor 项目,并开始设置我的第一个更复杂的允许/拒绝规则。我发现很难看出哪些允许触发,哪些不允许触发,以及这些函数中的某些变量包含什么。例如: List.allow({ u
我正在 AngularJS 中创建一个 Factory,它是这样的: if (href) { return $http({ method: method, url: item.href });
我是一名优秀的程序员,十分优秀!