karma-jasmine

how to test and resolve Controller data (.then function()) promise and get orginal Data in Jasmine2

大城市里の小女人 提交于 2019-11-27 04:54:22
问题 I am testing a controller that uses a service that returns a promise. I need to resolve promise. I am using Jasmine 2. Here is Spec code beforeEach(inject(function ($controller, $rootScope, _myService_, _$q_, _$rootScope_, _$httpBackend_, $http) { scope = $rootScope.$new(); $q = _$q_; $httpBackend = _$httpBackend_; $rootScope = _$rootScope_; myService = _myService_; $http = $http; ctrl = $controller('Ctrl', { '$scope': scope, 'myService': myService }); spyOn(myService, "getDateRangeData").and

Karma - Chrome failed 2 times (cannot start). Giving up

末鹿安然 提交于 2019-11-27 04:45:36
问题 I've been trying to run my tests using karma-chrome-launcher, but everytime I run my tests it throws this error: INFO [launcher]: Starting browser Chrome ERROR [launcher]: Cannot start Chrome INFO [launcher]: Trying to start Chrome again (1/2). ERROR [launcher]: Cannot start Chrome INFO [launcher]: Trying to start Chrome again (2/2). ERROR [launcher]: Cannot start Chrome ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up. Here's my karma.conf.js code: // Karma configuration //

Getting “Mismatched anonymous define() module…” when I try running tests

廉价感情. 提交于 2019-11-27 04:40:16
I am trying to configure my karma jasmine unit testing with requirejs. But each time i run it, i am getting the below error: Chrome 34.0.1847 (Mac OS X 10.9.2) ERROR Uncaught Error: Mismatched anonymous define() module: function (angular){ describe('Unit: Testing RequireJS', function(){ var ctrl; var scope; var rootScope; beforeEach(angular.mock.module('wsaApp')); beforeEach(angular.mock...<omitted>...ch Below are differenr file: spec file: define(['angular'], function(angular){ describe('Unit: Testing RequireJS', function(){ var ctrl; var scope; var rootScope; beforeEach(angular.mock.module(

Angular 2 Testing - Async function call - when to use

醉酒当歌 提交于 2019-11-27 04:09:16
When do you use the async function in the TestBed when testing in Angular 2? When do you use this? beforeEach(() => { TestBed.configureTestingModule({ declarations: [MyModule], schemas: [NO_ERRORS_SCHEMA], }); }); And when do you use this? beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [MyModule], schemas: [NO_ERRORS_SCHEMA], }); })); Can anyone enlighten me on this ? Paul Samsotha async will not allow the next test to start until the async finishes all its tasks. What async does is wrap the callback in a Zone, where all asynchronous tasks (e.g. setTimeout ) are

Angular 2/4/6/7 - Unit Testing with Router

半腔热情 提交于 2019-11-27 03:55:25
In Angular 2.0.0, I am unit testing a component that uses Router. However I get the 'Supplied parameters do not match any signature of call target.' error. In Visual studio code in spec.ts it is the new Router() that is highlighted in red I really appreciate if someone could let me know what the correct syntax would be? Thanks in advance. My code as follows: spec.ts import { TestBed, async } from '@angular/core/testing'; import { NavToolComponent } from './nav-tool.component'; import { ComponentComm } from '../../shared/component-comm.service'; import { Router } from '@angular/router';

What is the difference between fakeAsync and async in angular2 testing

妖精的绣舞 提交于 2019-11-27 03:48:34
问题 I know that tick() function utilizes fakeAsync . And also I can use fixture.whenStable().then() with async and fakeAsync as well. I want to know the exact use case for both of them. Can anyone explain this with examples. Note : I want to use Fake Service or Stub in both the scenarios. 回答1: For the most part they can be used interchangeably. I can't think of anything off the top of my head in which one is required over the other, except for the case of components whose external templates and

How to resolve promises in AngularJS, Jasmine 2.0 when there is no $scope to force a digest?

馋奶兔 提交于 2019-11-27 01:27:16
问题 It seems that promises do not resolve in Angular/Jasmine tests unless you force a $scope.$digest(). This is silly IMO but fine, I have that working where applicable (controllers). The situation I'm in now is I have a service which could care less about any scopes in the application, all it does it return some data from the server but the promise doesn't seem to be resolving. app.service('myService', function($q) { return { getSomething: function() { var deferred = $q.defer(); deferred.resolve

Angular unit-test controllers - mocking service inside controller

…衆ロ難τιáo~ 提交于 2019-11-27 00:30:53
问题 I have the following situation: controller.js controller('PublishersCtrl',['$scope','APIService','$timeout', function($scope,APIService,$timeout) { APIService.get_publisher_list().then(function(data){ }); })); controllerSpec.js 'use strict'; describe('controllers', function(){ var scope, ctrl, timeout; beforeEach(module('controllers')); beforeEach(inject(function($rootScope, $controller) { scope = $rootScope.$new(); // this is what you missed out timeout = {}; controller = $controller(

Angular 2 Final Release Router Unit Test

十年热恋 提交于 2019-11-26 19:58:05
问题 How do I unit test routers in Angular version 2.0.0 with karma and jasmine? Here's what my old unit test looks like in version 2.0.0-beta.14 import { it, inject, injectAsync, beforeEach, beforeEachProviders, TestComponentBuilder } from 'angular2/testing'; import { RootRouter } from 'angular2/src/router/router'; import { Location, RouteParams, Router, RouteRegistry, ROUTER_PRIMARY_COMPONENT } from 'angular2/router'; import { SpyLocation } from 'angular2/src/mock/location_mock'; import {

How do I debug a “[object ErrorEvent] thrown” error in my Karma/Jasmine tests?

谁都会走 提交于 2019-11-26 18:57:44
问题 I have several failing tests that only output [object ErrorEvent] thrown . I don't see anything in the console that helps me pinpoint the offending code. Is there something I need to do to track these down? [EDIT]: I'm running Karma v1.70, Jasmine v2.7.0 回答1: To fix that you have to run your tests without sourcemaps as a workaround: CLI v6.0.8 and above --source-map=false CLI v6.0.x early versions --sourceMap=false CLI v1.x --sourcemaps=false Shortcut ng test -sm=false might also work There