我想要得到承诺的结果before
摩卡 - 检索承诺测试的结果
describe('unsubscribe', function() {
var arn;
this.timeout(10000);
before(function(done) {
sns.subscribe('param1', 'param2').then(
(result) => {
arn = result;
done();
},
(error) => {
assert.fail(error);
done();
});
});
it('("param") => promise returns object', function() {
const result = sns.unsubscribe(arn);
expect(result).to.eventually.be.an('object');
});
});
同样,在after
我想获得承诺的结果,在测试
describe('subscribe', function() {
var arn;
this.timeout(10000);
it('("param1","param2") => promise returns string', function(done) {
sns.subscribe('param1', 'param2').then(
(result) => {
arn = result;
expect(result).to.be.a('string');
},
(error) => {
assert.fail(error);
done();
});
});
after(function(done) {
sns.unsubscribe(arn).then(
(result) => done());
});
});
这段代码写得好吗?有没有更好的做法?推荐的方式是什么?
谢谢你的回答。还有一个问题:如果测试承诺的返回结果有许多“期望”,那么怎么样?什么时候会被退回? – necroface
'chai-as-promised'的断言是承诺,所以你可以使用'then'将它们链接成任何承诺。你可能会返回expect(result).to.eventually.be.an(“object”)。then(()=> expect(otherResult).to.eventually.be.whatever)' – Louis