I ended up using the same trick I used to test $mdSidenav, but modified it just a little bit. In the interests of making it easier on myself next time I have to test $mdMedia, here we go.
var mediaMock, mediaQueryResult;
beforeEach(module(function ($provide) {
mediaMock = jasmine.createSpy('$mdMedia');
$provide.factory('$mdMedia', function() {
return function() {
return mediaQueryResult;
};
});
}));
it('should do something when $mdMedia() returns false regardless of what is passed to it', function() {
// arrange
mediaQueryResult = false;
// act
myFactory.myFunction();
// assert
expect(myFactory.myOtherFunction).toHaveBeenCalled();
});
it('should do something when $mdMedia() returns true regardless of what is passed to it', function() {
// arrange
mediaQueryResult = true;
// act
myFactory.myFunction();
// assert
expect(myFactory.myOtherFunction).not.toHaveBeenCalled();
});
That's all I had to do to get it to work. I'm sure there's a way to vary the result based on the parameter, but I didn't need to do that so I didn't solve that problem... yet.
No comments:
Post a Comment