karma-jasmine

how to remove Unexpected request: GET data.json?

非 Y 不嫁゛ 提交于 2020-01-03 03:48:25
问题 I am trying to use $httpBackend to test my $http request ..i AM getting this error Unexpected request: GET data.json No more request expected here is my testing code beforeEach(inject(function($rootScope,$controller,appfactory,_$httpBackend_) { $scope = $rootScope.$new(); $httpBackend=_$httpBackend_; ctrl = $controller('cntrl', {$scope: $scope}); fac=appfactory; modeSpy= spyOn(fac, 'mode').and.returnValue('a'); })); it('test true value',function(){ expect(true).toBeTruthy() }) it('check

Is it possible to mock local variable in angularjs factory from karma?

久未见 提交于 2020-01-03 03:14:07
问题 I have the below code: 'use strict'; angular .module('testmodule') .factory('TestService', ['$q', '$timeout', function ($q, $timeout) { var timeoutRetries = 0; // need to mock this from here var api = new TestApi(); function getResults(id, prevDeferred) { var deferred = prevDeferred || $q.defer(); function handleSuccessResponse(data) { if (data.status === 'ready') { results.put(id, data); deferred.resolve(data); } else { if (++timeoutRetries > 30) { // It wont get in here handleErrorResponse(

Unit Test Expect SpyOn Not Found

為{幸葍}努か 提交于 2020-01-02 13:30:14
问题 I have a directive (restrict A) that handles an event click, and calls a service based on a value. Directive: define(function () { 'use strict'; var myDirective = function ($rootScope, myFactory) { return { restrict: 'A', scope: { _myValue : '=value' }, link: function(scope, element, attrs) { element.bind('click', function() { if (scope._myValue === 'red') { myFactory.red(); } if (scope._myValue === 'green') { myFactory.green(); } if (scope._myValue === 'black') { myFactory.black(); } }); } }

How can I test $rootScope.$emit event?

最后都变了- 提交于 2020-01-02 07:05:25
问题 I have below code in abc controller: $rootScope.$on('selectedItem', function (event, data) { vm.selectedItem = data; }); And the caller function is in xyz controller: function doThis(){ $rootScope.$emit('selectedItem', 'somedata'); } How can I reproduce or mock this scenario in karma test? 回答1: For first controller ( abc ), where you listen to it using $rootScope.$on , you can first $rootScope.$emit it and $scope.$digest() it. So that you can receive it in $on . var rootScope; beforeEach

angular 2 testing w/router

断了今生、忘了曾经 提交于 2020-01-02 07:04:58
问题 I have a component and when a user logs in it routes to a url called /dashboard I am really struggling figuring out why I am getting the following error. cannot read property 'args' of undefined I have been following the official docs on testing with router found here https://angular.io/docs/ts/latest/guide/testing.html#!#routed-component but it doesnt seem to help. Half my problem is I dont quite understand all the code in the doc. Here is my unit test for the route beforeEach(async(()=>{

Unit testing with private service injected using jasmine angular2

巧了我就是萌 提交于 2020-01-02 02:21:26
问题 I have a problem trying to unit test an angular service. I want to verify that this service is properly calling another service that is injected into it. Lets say I have this ServiceToTest that injects ServiceInjected: ServiceToTest .service.ts @Injectable() export class ServiceToTest { constructor(private _si: ServiceInjected) {} public init() { this._si.configure(); } } ServiceInjected.service.ts @Injectable() export class ServiceInjected { constructor() {} public configure() { /*Some

Angular mock fails to inject my module dependencies

五迷三道 提交于 2020-01-02 01:07:18
问题 I want to test an Angular controller for my application fooApp , defined as follow: var fooApp = angular.module('fooApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]); ... The controller, MainCtrl is defined: "use strict"; fooApp.controller('MainCtrl', function ($scope, $rootScope, fooService) { ... } So I've tested several ways to create a test, like this one: 'use strict'; describe('MainController test', function () { var scope; var controller; beforeEach(function () { angular.mock.module(

Cannot make XHRs from within a fake async test

*爱你&永不变心* 提交于 2020-01-01 16:05:16
问题 Total novice at Karma/Jasmine looking for some assistance. I'm trying to run the following test and I get the error "Cannot make XHRs within a fake async test". I have included the test and the method I am attempting to call. Any help is greatly appreciated. import... fdescribe('CageService', () => { beforeEach(() => { TestBed.configureTestingModule({ imports: [ RouterTestingModule, HttpModule ], providers: [ BaseRequestOptions, MockBackend, CageService, { provide: 'appHttpService', useClass:

Debugging karma-jasmine tests with node-inspector

本小妞迷上赌 提交于 2020-01-01 10:47:10
问题 Almost the same question as Debugging jasmine-node tests with node-inspector BUT does anyone know how to use node-inspector with karma? 回答1: Install the node-inspector globally from NPM: npm install -g node-inspector Then start the node-inspector and background the process (use fg to bring it back to the foreground and kill %1 to stop it): node-inspector & And then start your test runner like in debug mode this node --inspect ./node_modules/karma/bin/karma start Then connect to the inspector

AngularJS Unit Testing - Various patterns for injecting dependencies

▼魔方 西西 提交于 2020-01-01 10:03:40
问题 I'm new to unit testing and am mainly learning from examples that I find. The problem is that I've seen so many different patterns that it's hard to understand what the differences are between them. And how to combine those patterns for various use cases. Below is one such pattern: var $rootScope, $window, $location; beforeEach(angular.mock.module('security.service', 'security/loginModal.tpl.html')); beforeEach(inject(function(_$rootScope_, _$location_) { $rootScope = _$rootScope_; $location