cho*_*ovy 140 mocha.js node.js promise chai
以下测试表现得很奇怪:
it('Should return the exchange rates for btc_ltc', function(done) {
var pair = 'btc_ltc';
shapeshift.getRate(pair)
.then(function(data){
expect(data.pair).to.equal(pair);
expect(data.rate).to.have.length(400);
done();
})
.catch(function(err){
//this should really be `.catch` for a failed request, but
//instead it looks like chai is picking this up when a test fails
done(err);
})
});
Run Code Online (Sandbox Code Playgroud)
我该如何妥善处理被拒绝的承诺(并对其进行测试)?
我该如何正确处理失败的测试(即:expect(data.rate).to.have.length(400);
?
这是我正在测试的实现:
var requestp = require('request-promise');
var shapeshift = module.exports = {};
var url = 'http://shapeshift.io';
shapeshift.getRate = function(pair){
return requestp({
url: url + '/rate/' + pair,
json: true
});
};
Run Code Online (Sandbox Code Playgroud)
Ben*_*aum 219
最简单的方法是使用Mocha在最近版本中内置的promises支持:
it('Should return the exchange rates for btc_ltc', function() { // no done
var pair = 'btc_ltc';
// note the return
return shapeshift.getRate(pair).then(function(data){
expect(data.pair).to.equal(pair);
expect(data.rate).to.have.length(400);
});// no catch, it'll figure it out since the promise is rejected
});
Run Code Online (Sandbox Code Playgroud)
或者使用现代Node和async/await:
it('Should return the exchange rates for btc_ltc', async () => { // no done
const pair = 'btc_ltc';
const data = await shapeshift.getRate(pair);
expect(data.pair).to.equal(pair);
expect(data.rate).to.have.length(400);
});
Run Code Online (Sandbox Code Playgroud)
由于这种方法是端到端的承诺,因此更容易测试,你不必考虑你正在考虑的奇怪案例,就像done()
各处的奇怪电话一样.
这是Mocha目前在Jasmine等其他图书馆的优势.您可能还想检查Chai As Promised,这会让它更容易(不.then
)但我个人更喜欢当前版本的清晰度和简洁性
fea*_*ool 40
正如已经在这里指出的那样,较新版本的Mocha已经是Promise-aware.但是由于OP专门询问了Chai,所以指出chai-as-promised
为测试承诺提供干净语法的软件包是公平的:
以下是如何使用chai-as-promised来测试Promise的两种情况resolve
和reject
案例:
var chai = require('chai');
var expect = chai.expect;
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
...
it('resolves as promised', function() {
return expect(Promise.resolve('woof')).to.eventually.equal('woof');
});
it('rejects as promised', function() {
return expect(Promise.reject('caw')).to.be.rejectedWith('caw');
});
Run Code Online (Sandbox Code Playgroud)
为了清楚地说明测试的内容,这里的编码相同的例子没有chai-as-promise:
it('resolves as promised', function() {
return Promise.resolve("woof")
.then(function(m) { expect(m).to.equal('woof'); })
.catch(function(m) { throw new Error('was not supposed to fail'); })
;
});
it('rejects as promised', function() {
return Promise.reject("caw")
.then(function(m) { throw new Error('was not supposed to succeed'); })
.catch(function(m) { expect(m).to.equal('caw'); })
;
});
Run Code Online (Sandbox Code Playgroud)
async/await
延迟的 Promise 函数,如果延迟为 0,则失败:
\n\nconst timeoutPromise = (time) => {\n return new Promise((resolve, reject) => {\n if (time === 0)\n reject({ 'message': 'invalid time 0' })\n setTimeout(() => resolve('done', time))\n })\n}\n\n// \xe2\x86\x93 \xe2\x86\x93 \xe2\x86\x93\nit('promise selftest', async () => {\n\n // positive test\n let r = await timeoutPromise(500)\n assert.equal(r, 'done')\n\n // negative test\n try {\n await timeoutPromise(0)\n // a failing assert here is a bad idea, since it would lead into the catch clause\xe2\x80\xa6\n } catch (err) {\n // optional, check for specific error (or error.type, error. message to contain \xe2\x80\xa6)\n assert.deepEqual(err, { 'message': 'invalid time 0' })\n return // this is important\n }\n assert.isOk(false, 'timeOut must throw')\n log('last')\n})\n
Run Code Online (Sandbox Code Playgroud)\n\n阳性测试相当简单。意外失败(通过模拟500\xe2\x86\x920
)将自动使测试失败。
负测试使用 try-catch-idea。但是:“抱怨”不期望的传递仅发生在 catch 子句之后(这样,它就不会出现在 catch() 子句中,从而触发进一步但误导性的错误。
\n\n为了使这一策略发挥作用,必须从 catch 子句返回测试。如果您不想测试其他任何内容,请使用另一个 it() 块。
\n 归档时间: |
|
查看次数: |
80818 次 |
最近记录: |