角度アプリの単体テストを書く際に予期しない結果が発生しました。予期しない動作をサンプルテストに凝縮することができました。失敗したアサーションが約束を引き起こすのはなぜですか?
thenブロックのアサーションエラーは、約束のキャッチブロックを引き起こすようです。私はこのテストを実行すると
describe.only('test', function() {
var $q, $rootScope;
beforeEach(function() {
inject(function(_$q_, _$rootScope_) {
$q = _$q_;
$rootScope = _$rootScope_.$new();
});
});
var stubService = sinon.stub(service, 'getPanel');
it('shall...', function() {
//1
$q.when().then(function() {
console.log('log then')
should.equal(true, false, 'should then') //<---assertion fails
}).catch(function() {
console.log('log catch') //<--- why does this block run?
should.equal(true, false, 'should catch')
})
$rootScope.$apply(); //wait for promises to finish
});
});
出力は次のとおりです。
LOG LOG: 'log then'
LOG LOG: 'log catch'
test
✗ shall...
should catch: expected true to equal false
私は予想:
LOG LOG: 'log then'
test
✗ shall...
should then: expected true to equal false
私は私が期待した結果を得るこのスタイルを使用する場合:
$q.when().then(function() {
console.log('log then')
should.equal(true, false, 'should then')
}, function() {
console.log('log catch')
should.equal(true, false, 'should catch')
})
を
私の会社のコンベンションは、私は可能な場合は最初のスタイルを使用したいと思います。