how to mock a scope from a directive instead of a controller in angular unit-test
Date : March 29 2020, 07:55 AM
Does that help so i am familiar with the concept of mocking a $scope and controller with a $controller constructor , The way I set up directive-specs is beforeEach(inject(function($rootScope, $compile) {
var htmlString = '' +
'<my-directive some-attr="value">' +
'</my-directive>'
;
element = angular.element(htmlString)
scope = $rootScope.$new();
$compile(element)(scope);
scope.$digest();
}));
|
Mocking Controller Instantiation In Angular Directive Unit Test
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can create mocks in module configuration block by using $controllerProvider.register() for controllers, $provide.provider(), $provide.factory(), $provide.service() and $provide.value() for providers, factories and services: JavaScript beforeEach(function () {
module('App.Directives.BreadCrumbs', function($provide, $controllerProvider) {
$controllerProvider.register('BreadCrumbsController', function($scope) {
// Controller Mock
});
$provide.factory('someService', function() {
// Service/Factory Mock
return {
doSomething: function() {}
}
});
});
});
|
How to Unit Test Angular with Jasmine Controller inside a Directive
Date : March 29 2020, 07:55 AM
may help you . You have to initialize and inject your controller as well. Something like this: var $controller;
var $rootScope;
var scope;
var controller;
beforeEach(inject(function (_$controller_, _$rootScope_) {
$controller = _$controller_;
$rootScope = _$rootScope_;
scope = $rootScope.$new();
controller = $controller('ScopeController', { '$scope': scope });
}));
|
How to unit test Angular routeChangeSuccess in directive link function?
Date : March 29 2020, 07:55 AM
I wish this helpful for you I eventually figured out how to trigger the route change using $route.reload() From the docs, it: describe('sidebar directive', function () {
var $compile,
$rootScope,
$route,
$location,
scope,
element;
beforeEach(module('MyApp'));
beforeEach(module('my.templates')); // ng-html2js karma template loader
beforeEach(inject(function(_$compile_, _$rootScope_, _$route_, _$location_){
$compile = _$compile_;
$rootScope = _$rootScope_;
$location = _$location_;
$route = _$route_;
scope = $rootScope.$new();
element = $compile("<sidebar></sidebar>")(scope);
}));
it('defaults to /about route', function() {
$route.reload();
$rootScope.$digest();
var linkScope = element.children().scope(); // Get directive's isolated scope
expect(linkScope.activeNav).toBe('/about');
});
it('sets activeNave to correct route on change', function() {
$location.path('/foo');
$route.reload();
$rootScope.$digest();
var linkScope = element.children().scope();
expect(linkScope.activeNav).toBe('/foo');
});
});
|
Angular 4 Jasmine Unit Test Directive Export Function
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I have a created custom directive in angular 4 the directive is below. , Coverage is easy it('should cover the function', () => {
appValidator(new FormControl(''));
});
|