Angular 测试文档显示了一个测试示例,其中 $apply 在测试断言之前被调用。我尝试做同样的事情,但我的测试无法正常工作。第一个应该中断的代码可以工作……看来我的断言没有运行。第二个代码,我的测试有效,但它与文档不同(我更喜欢这种风格)。
第一的:
it('tests angular promises', function() {
getABC = function() {
return $q((resolve, reject) => {
resolve('abc');
});
};
var state = getABC()
$rootScope.$apply()
// assertions come after $apply call as in documentation: doesn't work.
state.should.eventually.be.equal('abcc')
})
------
✓ tests angular promises
1 passing (78ms)
第二
it('tests angular promises', function() {
getABC = function() {
return $q((resolve, reject) => {
resolve('abc');
});
};
var state = getABC()
state.should.eventually.be.equal('abcc')
// assertions come before $apply call: works
$rootScope.$apply()
})
------
1 failing
1) tests angular promises:
AssertionError: expected 'abc' to equal 'abcc'
+ expected - actual
-abc
+abcc