jasmine

How to mock/trigger a $routeChangeSuccess in Angular unit tests?

流过昼夜 提交于 2019-12-08 14:45:55
问题 Given a handler attached to the $routeChangeSuccess event to update the title property on $rootScope: $rootScope.$on('$routeChangeSuccess', function (event, current, previous) { $rootScope.title = 'My title'; }); in unit tests, I expect that performing a route change would update the title property: it('should set a title on $routeChangeSuccess', function () { $location.path('/contacts'); $rootScope.$apply(); expect($rootScope.title).toBe('My title'); }); However, the code in the

Infinite jasmine timeout

情到浓时终转凉″ 提交于 2019-12-08 14:40:12
问题 This is basically a follow-up to Remove timeout for single jasmine spec github issue. The question: Is it possible to make a single test never timeout? The problem: It is possible to set a timeout value globally via DEFAULT_TIMEOUT_INTERVAL or for every describe with beforeEach / afterEach or on a single it() block: it('Has a custom timeout', function() { expect(true).toBeTruthy(); }, value in msec) I'm interested in having a single spec never timeout. I've tried to follow the advice proposed

Jasmine object “has no method 'andReturn'”

依然范特西╮ 提交于 2019-12-08 14:29:59
问题 Beginner with Jasmine, very first attempt with Jasmine Spies. I thought I was mimicking the format displayed here (search: "andReturn"), but I'm getting an error that I can't work out: TypeError: Object function () { callTracker.track({ object: this, args: Array.prototype.slice.apply(arguments) }); return spyStrategy.exec.apply(this, arguments); } has no method 'andReturn' No clue what I'm doing wrong. Here's my Spec: describe('Die', function() { it('returns a value when you roll it',

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,

Test fails, how to find out why?

若如初见. 提交于 2019-12-08 13:24:00
问题 I am trying to learn testing for angular using Jasmine and Karma, but I have runned into a bit of problem trying to create my first test. First i got this very simple angular app with a controller named "testCtrl". angular.module("testApp", []) .controller("testCtrl", function() { this.title = "The Title"; }); I then got this very simple index.html file <body data-ng-app="testApp"> <div data-ng-controller="testCtrl as test"> {{test.title}} </div> </body> Okay so this far everything works fine

Jasmine - Unble to get DOM elements

蹲街弑〆低调 提交于 2019-12-08 12:50:46
问题 I am testing an angularjs directive that manipulates the DOM. I am trying to get the element in my Jasmine spec, so that I can test the functionality of the directive. However, when I use document.getElementsByClassName or TagName or ID , it doesn't return anything. Does anyone have ideas about this? html = document.getElementsByClassName('analog'); console.dir(html); 回答1: If you create a test in headless browser/chrome etc., you could append a dummy object, for example JQuery node, then

Count the occurrence of each word in a phrase using javascript

和自甴很熟 提交于 2019-12-08 12:46:17
问题 For example for the input "olly olly in come free" The program should return: olly: 2 in: 1 come: 1 free: 1 The tests are written as: var words = require('./word-count'); describe("words()", function() { it("counts one word", function() { var expectedCounts = { word: 1 }; expect(words("word")).toEqual(expectedCounts); }); //more tests here }); How do I start in my word-count.js file? Create a method words() or a module Words() and make an expectedCount method in there and export it? Do I

Click into an icon isn't working on protractor

亡梦爱人 提交于 2019-12-08 12:02:07
问题 I have an icon to add a new item, i want to click into it but the action isn't done. I don't know if i'm locating the element in a right way ? I have tried the answer provided in that Question : element(by.css('[ng-click="createWL()"]')).click(); element(by.css("button[ng-click*=createWL]")).click(); But it didn't work for me. Here my html code : <button class="md-icon-button watchlist-icon-button md-button md-ink-ripple" type="button" ng-transclude="" id="addWL" aria-label="Add Watchlist" ng

Angular2 - HTTP call unit testing

霸气de小男生 提交于 2019-12-08 11:58:17
问题 I am a beginner of angular2. My service.ts will be, import { Injectable } from '@angular/core'; import { Http, Response } from '@angular/http'; import {Headers, RequestOptions, URLSearchParams} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import 'rxjs/add/observable/throw'; @Injectable() export class HomeService { url : string; constructor(private _http: Http) {} getUserInfo(userId : string) { this.url =

Jasmine spyOn javascript getter causing karma to hang

荒凉一梦 提交于 2019-12-08 11:15:15
问题 I'm having some issues using Jasmine to write tests which spy on a Javascript getter. It causes my test suite to hang (using karma + phantomJS) and then ultimately the browser disconnects having never progressed further than the test in question. A simple example is probably the easiest way to explain (using ES6 transpiled with webpack + babel-loader): class ExampleClass { get name() { return "My Name"; } } In order to change what this get method returns for my test, I am trying the following