Skip to content

Instantly share code, notes, and snippets.

@trodrigues
Last active August 30, 2019 03:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save trodrigues/7414091 to your computer and use it in GitHub Desktop.
Save trodrigues/7414091 to your computer and use it in GitHub Desktop.
Angular.js directive test example with service mocking
describe('Directive test', function () {
var container, scope;
var aStub;
beforeEach(function () {
aStub = sinon.stub();
module('testmodule', function ($provide) {
$provide.value('someService', aStub);
});
inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
// controller for this directive uses the stubbed service
// to determine if a button should be shown
container = $('<some-directive></some-directive>');
$compile(container)(scope);
scope.$digest();
});
});
afterEach(function () {
container.remove();
});
it('do not show a button depending on service', function () {
aStub.returns(false);
scope.$apply();
expect(container.find('.button').hasClass('ng-hide')).toBe(true);
});
it('show a button depending on service', function () {
aStub.returns(true);
scope.$apply();
expect(container.find('.button').hasClass('ng-hide')).toBe(false);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment