我想要得到承諾的結果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