Tyl*_*ich 30 javascript jasmine angularjs
我一直试图围绕Jasmine 2.0和AngularJS承诺.我知道:
done功能,以取代旧的runs和waitsFor功能$q承诺在触发摘要周期之前不会解决如何在Jasmine 2.0中使用新的异步语法测试AngularJS承诺?
Tyl*_*ich 43
致电后promise.resolve():
$timeout.flush().这将强制摘要周期并传播承诺解决方案done().这告诉Jasmine异步测试已经完成这是一个例子(关于Plunker的演示):
describe('AngularJS promises and Jasmine 2.0', function() {
var $q, $timeout;
beforeEach(inject(function(_$q_, _$timeout_) {
// Set `$q` and `$timeout` before tests run
$q = _$q_;
$timeout = _$timeout_;
}));
// Putting `done` as argument allows async testing
it('Demonstrates asynchronous testing', function(done) {
var deferred = $q.defer();
$timeout(function() {
deferred.resolve('I told you I would come!');
}, 1000); // This won't actually wait for 1 second.
// `$timeout.flush()` will force it to execute.
deferred.promise.then(function(value) {
// Tests set within `then` function of promise
expect(value).toBe('I told you I would come!');
})
// IMPORTANT: `done` must be called after promise is resolved
.finally(done);
$timeout.flush(); // Force digest cycle to resolve promises
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12940 次 |
| 最近记录: |