ngAnimate 1.4.7 单元测试不调用动画函数
ngAnimate 1.4.7 unit test not calling animation functions
我一直在 this tutorial 工作,并且用谷歌搜索了令人作呕的内容,但我似乎无法得到看似微不足道的 ngAnimate
单元测试 运行。
我 ngAnimate
在应用程序中运行良好。所有 Angular 核心库均为 1.4.7 版。
模块
angular.module 'MyAnimation', [ 'ngAnimate' ]
.animation '.added-class', ->
addClass: (element, className, done) ->
console.log 'add class triggered'
element.css 'opacity', '0.5'
done()
测试
describe 'MyAnimation', ->
beforeEach -> module 'ngAnimate'
beforeEach -> module 'ngAnimateMock'
beforeEach -> module 'MyAnimation'
it 'animates', -> inject ($animate, $rootScope, $rootElement) ->
$animate.enabled(true)
divElement = angular.element '<div>my div</div>'
# Kick off initial digest loop in which no animations are run.
$rootScope.$digest()
# Trigger animation.
$animate.addClass divElement, 'added-class'
$rootScope.$digest()
# Tried this, doesn't seem to do anything.
# $animate.flush()
# Results in AssertionError: expected '' to equal '0.5'
expect(divElement.css('opacity')).to.eq '0.5'
我确定该模块已包含在测试中,但触发 $animate.enter
甚至没有得到我的 log
输出。
我也尝试过使用其他 $animate
函数,但毫无进展。帮忙?
在认真研究 Angular 的源代码后,罪魁祸首似乎是内部检查 areAnimationsAllowed
,Angular 使用它来确定是否提前中止动画。除其他事项外,它 检查动画节点是否是 $rootElement
和文档主体的后代。
这里有两个选择。
- Plunker。 将要设置动画的节点附加到
$rootElement
并将 $rootElement
附加到主体 。后者是必要的,因为 ngMock
实际上存根 $rootElement
与一个分离的 <div>
节点保存在内存中。 示例:
var element, body, root;
beforeEach(module('ngAnimate', 'ngAnimateMock', 'MyAnimation'));
beforeEach(inject(function ($animate, $document, $rootElement, $rootScope) {
// enable animations globally
$animate.enabled(true);
// create a node to be animated and inject it into the DOM
element = angular.element('<div></div>');
root = $rootElement.append(element)[0];
body = $document[0].body;
body.appendChild(root);
// trigger initial digest
$rootScope.$digest();
}));
afterEach(function () {
// clean up
body.removeChild(root);
});
- Plunker. Do not test your animation using
$animate.addClass
, but instead use the lower-level $$animateJs
service. Angular uses it inside their own tests,我假设绕过了上面的检查。 示例:
it('should animate', inject(function ($animate, $$animateJs) {
// trigger animation
$$animateJs(element, 'addClass', {
addClass: 'added-class'
}).start();
$animate.flush();
}));
如果您 运行 Plunkers 并检查控制台,您应该会看到 "addClass triggered" 消息。我还添加了一些断言。
这两种解决方案都是 hacky 和未记录的,如果您的 addClass
动画做一些异步的事情(我认为它会),这两种解决方案都会变得更加复杂。不幸的是,我找不到更好的方法来测试 JS 动画。过去我实际上忽略了覆盖范围内的动画代码,因为它很难测试。
我一直在 this tutorial 工作,并且用谷歌搜索了令人作呕的内容,但我似乎无法得到看似微不足道的 ngAnimate
单元测试 运行。
我 ngAnimate
在应用程序中运行良好。所有 Angular 核心库均为 1.4.7 版。
模块
angular.module 'MyAnimation', [ 'ngAnimate' ]
.animation '.added-class', ->
addClass: (element, className, done) ->
console.log 'add class triggered'
element.css 'opacity', '0.5'
done()
测试
describe 'MyAnimation', ->
beforeEach -> module 'ngAnimate'
beforeEach -> module 'ngAnimateMock'
beforeEach -> module 'MyAnimation'
it 'animates', -> inject ($animate, $rootScope, $rootElement) ->
$animate.enabled(true)
divElement = angular.element '<div>my div</div>'
# Kick off initial digest loop in which no animations are run.
$rootScope.$digest()
# Trigger animation.
$animate.addClass divElement, 'added-class'
$rootScope.$digest()
# Tried this, doesn't seem to do anything.
# $animate.flush()
# Results in AssertionError: expected '' to equal '0.5'
expect(divElement.css('opacity')).to.eq '0.5'
我确定该模块已包含在测试中,但触发 $animate.enter
甚至没有得到我的 log
输出。
我也尝试过使用其他 $animate
函数,但毫无进展。帮忙?
在认真研究 Angular 的源代码后,罪魁祸首似乎是内部检查 areAnimationsAllowed
,Angular 使用它来确定是否提前中止动画。除其他事项外,它 检查动画节点是否是 $rootElement
和文档主体的后代。
这里有两个选择。
- Plunker。 将要设置动画的节点附加到
$rootElement
并将$rootElement
附加到主体 。后者是必要的,因为ngMock
实际上存根$rootElement
与一个分离的<div>
节点保存在内存中。 示例:
var element, body, root;
beforeEach(module('ngAnimate', 'ngAnimateMock', 'MyAnimation'));
beforeEach(inject(function ($animate, $document, $rootElement, $rootScope) {
// enable animations globally
$animate.enabled(true);
// create a node to be animated and inject it into the DOM
element = angular.element('<div></div>');
root = $rootElement.append(element)[0];
body = $document[0].body;
body.appendChild(root);
// trigger initial digest
$rootScope.$digest();
}));
afterEach(function () {
// clean up
body.removeChild(root);
});
- Plunker. Do not test your animation using
$animate.addClass
, but instead use the lower-level$$animateJs
service. Angular uses it inside their own tests,我假设绕过了上面的检查。 示例:
it('should animate', inject(function ($animate, $$animateJs) {
// trigger animation
$$animateJs(element, 'addClass', {
addClass: 'added-class'
}).start();
$animate.flush();
}));
如果您 运行 Plunkers 并检查控制台,您应该会看到 "addClass triggered" 消息。我还添加了一些断言。
这两种解决方案都是 hacky 和未记录的,如果您的 addClass
动画做一些异步的事情(我认为它会),这两种解决方案都会变得更加复杂。不幸的是,我找不到更好的方法来测试 JS 动画。过去我实际上忽略了覆盖范围内的动画代码,因为它很难测试。