karma-jasmine

Mock custom service in angular2 during unit test

╄→гoц情女王★ 提交于 2019-12-09 08:09:09
问题 I'm trying to write a unit test for component used in my service. Component and service work fine. Component: import {Component} from '@angular/core'; import {PonyService} from '../../services'; import {Pony} from "../../models/pony.model"; @Component({ selector: 'el-ponies', templateUrl: 'ponies.component.html', providers: [PonyService] }) export class PoniesComponent { ponies: Array<Pony>; constructor(private ponyService: PonyService) { this.ponies = this.ponyService.getPonies(2); }

Angular2 material 'md-icon' is not a known element with Karma / Jasmine

淺唱寂寞╮ 提交于 2019-12-09 03:06:06
问题 I'm working on an Angular2 application using @angular/material 2.0.0-alpha.11-3 angular-cli 1.0.0-beta.19-3 karma 1.2.0 karma-jasmine 1.0.2 Running it works fine but a couple of the tests where the template has a button with md-icon fail with template errors: ERROR: 'Unhandled Promise rejection:', 'Template parse errors: 'md-icon' is not a known element: 1. If 'md-icon' is an Angular component, then verify that it is part of this module. 2. If 'md-icon' is a Web Component then add "CUSTOM

Failed: Can't resolve all parameters for MatDialogRef: (?, ?, ?). unit testing Angular project

杀马特。学长 韩版系。学妹 提交于 2019-12-09 02:38:29
问题 I am new to angular development and more new towards unit testing using jasmine. I have created a component to sow a dialog using angular material MatDialogRef, MAT_DIALOG_DATA from @angular/material. The component is working fine but the Unit testing is giving me an error which i am not able to resolve. I really need this to work and any help would be appreciated.... Thanks in advance..!!! Please find my code below: app.module.ts import { BrowserModule } from '@angular/platform-browser';

Disconnected (1 times), because no message in 10000 ms using Karma-Jasmine

左心房为你撑大大i 提交于 2019-12-09 02:20:04
问题 Using Karma runner with jasmine. After all the configuration done, I type on terminal the below command: karma start public/javascripts/karma.conf.js But I am getting the below error where my browser is getting closed. INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/ INFO [launcher]: Starting browser Chrome INFO [Chrome 34.0.1847 (Mac OS X 10.9.2)]: Connected on socket hDO3pMdVNGcBMDx4FI0w with id 60695552 WARN [Chrome 34.0.1847 (Mac OS X 10.9.2)]: Disconnected (1 times),

Karma cannot load Angular 2 Component

不想你离开。 提交于 2019-12-08 18:19:29
My Karma cannont load angular 2 component. Drop this error: [web-server] :404 : /base/src/client/app/container/container.component i do everything with angular 2 testing tutorial (i only modified path in karma) So problem must be in path. but i don't have any idea how to fix it. My Path: src | |client | | |container | | | |container.component.ts | | | |container.component.spec.ts |karma.conf.js |karma-test-shim.js container.component.ts import { Component, OnInit } from '@angular/core'; import { UserService } from '../user/user.service'; import { User } from '../user/user'; //import {

how to remove Unexpected request: GET data.json?

こ雲淡風輕ζ 提交于 2019-12-08 16:30:31
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 message value',function(){ $scope.toggle(); expect($scope.message).toEqual('naveen') }) it("tracks that the

karma + jasmine + webpack: module is not a function

对着背影说爱祢 提交于 2019-12-08 15:16:56
问题 I cannot instantiate a controller because of this error, it says: module is not a function What module is supposed to do is being an alias of angular.mock, but my question is: Can it be that module gets rewrite but module from webpack? (module.exports) This is my karma.config.js file: /*global __dirname*/ // Karma configuration var path = require('path'); var webpackConfig = require('./webpack.config'); module.exports = function(config) { config.set({ // base path that will be used to resolve

How to unit test a Angular 4 component which uses router.paramMap

守給你的承諾、 提交于 2019-12-08 15:13:21
问题 I m new to angular 4 and trying to test one of the angular 4 feature router.paramMap from unit tests, Reading route params in fallowing way and working as expected in my application. constructor(private router:Router, private actRoute:ActivatedRoute) { } ngOnInit() { this.router.paramMap.subscribe(params => { params.get(id); }) ...... } But while running unit test, i m getting error which says cannot call subscribe method of undefined even though i m passing passing route param as below. {

Issues getting Karma to work with SystemJS, Angular2 and Typescript

拈花ヽ惹草 提交于 2019-12-08 15:09:37
问题 I am facing issues getting Karma to work with SystemJS, Angular2 and Typescript. Here is my karma.conf.js: 'use strict'; module.exports = function (config) { config.set({ // base path, that will be used to resolve files and exclude basePath: '.', // frameworks to use frameworks: ['systemjs', 'jasmine'], plugins: ['karma-systemjs', 'karma-jasmine', 'karma-phantomjs-launcher'], files: [ 'node_modules/reflect-metadata/Reflect.js', 'app/**/*.spec.ts', //'jspm_packages/system-polyfills.js', 'karma

mock http request in Angular 4

喜欢而已 提交于 2019-12-08 13:29:10
问题 I write an app in Angular 4 and I've got ngOnInit function with http get request. I want to write unit test, but don't know how to mock the request. ngOnInit() { this.http.get<any>('someurl', { headers: new HttpHeaders().set('Authorization', 'authorization'`) }) .subscribe( (res) => { this.servB = res.items .map((item) => { return new ServB(item) }); } ); } and my unit test so far looks like this: beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ HttpClientModule,