问题
I'm having trouble testing my directive with an ng-mouseenter directive.
I'd like to test several things, but first off, I need to test that the method supplied to ng-mouseenter is called.
my test:
describe('hover tests', function () {
it('the triggerPopover method should be called on hover', function() {
spyOn($scope, 'triggerPopover');
var ars = jQuery(view.find('article.the-class-im-looking-for'));
jQuery(ars[0]).trigger('mouseenter');
expect($scope.triggerPopover).toHaveBeenCalled();
});
});
my directive use:
<article my-directive ng-mouseenter="triggerPopover();"></article>
Result:
Expected spy triggerPopover to have been called. The ng-mouseenter stuff doesn't seem to get called
回答1:
If you are using PhantomJS for your tests, some mouse events that work in normal browsers will not work. If indeed that's the case, you can run your test with Chrome or Firefox, or alternatively implement a similar solution to this answer for a related question.
回答2:
mouseenter
as a native DOM event is not supported by all browsers: http://www.quirksmode.org/dom/events/index.html
ng-mouseenter
builds on the mouseover
event, so you can just trigger it like this:
jQuery(ars[0]).trigger('mouseover');
来源:https://stackoverflow.com/questions/26265900/jasmine-test-for-ng-mouseenter