karma-jasmine

Testing FormArray

故事扮演 提交于 2019-12-06 04:19:27
问题 I have a PhoneNumbersFormComponent whose template looks like : <div [formGroup]="form"> <div formArrayName="numbers"> <input formControlName="countryPrefix"> <input formControlName="number"> </div> </div> I want to pass the default test created by angular-cli's ng g component xxx The first error I got was: Cannot find control with name 'countryPrefix' Which I solved with: beforeEach(() => { fixture = TestBed.createComponent(PhoneNumbersFormComponent); component = fixture.componentInstance;

Angular testing: “Illegal state: Could not load the summary for directive” for an empty component

佐手、 提交于 2019-12-06 01:54:04
None of my components were passing unit tests, so I made a new one in the project to try and diagnose. import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-thing', templateUrl: './thing.component.html', styleUrls: ['./thing.component.scss'] }) export class ThingComponent implements OnInit { constructor() { } ngOnInit() { } } And a thing.component.spec.ts file as follows describe('ThingComponent', () => { let component: ThingComponent; let fixture: ComponentFixture<ThingComponent>; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ ThingComponent

angular 2 testing w/router

耗尽温柔 提交于 2019-12-06 01:08:28
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(()=>{ class AuthStub{ private loggedin: boolean = false; login(){ return this.loggedin = true; } }; class

Flushing successful mock POST request using Jasmine does not execute the AngularJS success function

…衆ロ難τιáo~ 提交于 2019-12-06 00:47:13
This is my AngularJS post.js : angular.module("PostPageApp", ["BaseApp"]) .controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) { var self = this; self.add = function() { BaseService.add.post(self.post, function() { self.cerrorMessages = BaseService.cerrorMessages; }); }; }]); This is base.js : angular.module("BaseApp", []) .config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.xsrfCookieName = 'csrftoken'; $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; }]) .config(['$locationProvider', function($locationProvider){

Reflect.getOwnMetadata is not a function with karma-typescript

时光怂恿深爱的人放手 提交于 2019-12-06 00:46:01
I am trying to unit test (with Karma + Jasmine + karma-typescript ) my TypeScript project. The project structure is as follows: root |- src/*.ts //all TypeScript source files |- tests/unit/*.spec.ts //all spec (test) files |- karma.conf.js |- tsconfig.json My karma.conf.js looks like following: module.exports = function (config) { config.set({ basePath: '', frameworks: ['jasmine', "karma-typescript"], karmaTypescriptConfig: { tsconfig: "./tsconfig.json" }, files: [ 'src/*.ts', 'tests/**/*Spec.ts' ], exclude: [], preprocessors: { "**/*.ts": ["karma-typescript"] }, reporters: ["progress", "karma

AngularJS: how to test directive which gives focus to element?

天大地大妈咪最大 提交于 2019-12-05 22:55:12
Note: the suggested link is an answer to a question about a service, and doesn't give a clear explanation on how to solve this issue I'm trying to setup a karma test for my simple (and working) AngularJS autofocus directive: app.directive('autofocus', function ($timeout) { return { replace: false, link: function (scope, element, attr) { scope.$watch(attr.autofocus, function (value) { if (value) { $timeout(function () { element[0].focus(); console.log('focus called'); }); } } ); } }; }); This is my current test: describe('The autofocus directive', function () { var timeout; beforeEach(module(

How can I test $rootScope.$emit event?

安稳与你 提交于 2019-12-05 22:27:19
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? 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(inject(function(_$rootScope_) { rootScope = _$rootScope_; })); describe("some function", function() { it("should

How to load mock data from JSON file in Angular 2 karma jasmine test?

你离开我真会死。 提交于 2019-12-05 21:43:54
问题 I writing karma jasmine test case for angular 2 , And we came across requirement to mock data in separate JSON file as data is huge (want to make sure code cleanness). For this I searched a lot but didn't find proper solution. We already mocking HTTP service using MockBackend , So we can't use HTTP service of angular to load JSON as it eventually request will go to MockBackend. So is there any another way without using any third party lib like jasmine-jquery or Karma-jasmine-preprocessor ?

How to test an Angular controller with a function that makes an AJAX call without mocking the AJAX call?

安稳与你 提交于 2019-12-05 19:33:21
I have a function in a controller that calls a service which in turn calls another service, which makes the AJAX call and returns the promise. To put it simply: MainController call = function(){ //Handle the promise here, update some configs and stuff someService.call().then(success, failure); } SomeService return { call: function(){ return SomeOtherService.call(); } } SomeOtherService return { call: function(){ return $http.get(someUrl); } } How do I test the main controller's call function without actually making the AJAX call? I will obviously be testing the services separately. Update: So

Angular Testing: Spy a function that was executed on the initialize of a controller

旧巷老猫 提交于 2019-12-05 16:04:08
问题 I've been trying to spy a function that was executed on the initialize of a controller, but the test always failed. I've been trying execute $scope.$digest() and this it's not working, However in the console, i see that the function have been called. I can't figure out this, Someone can explain to me why this it's not working? Codepen Example: http://codepen.io/gpincheiraa/pen/KzZNby Controller function Controller($stateParams, $scope){ $scope.requestAuthorization = requestAuthorization; if (