Questions tagged [angular2-testing]
289 questions
29
votes
1 answer
Mocking service in a component - mock ignored
This time I'm trying to mock a service (that does http calls) to test a component.
@Component({
selector: 'ub-funding-plan',
templateUrl: './funding-plan.component.html',
styleUrls: ['./funding-plan.component.css'],
providers:…

youri
- 3,685
- 5
- 23
- 43
27
votes
2 answers
How to unit test if an angular 2 component contains another component
I'm quite new to angular 2.
I have a component which in turn has some other components in its template.
How do I write unit tests to check if my parent component consists of other components.
Mentioning a sample or directing me to a resource would…

Chan15
- 929
- 2
- 9
- 16
27
votes
1 answer
Angular2 how to unit test a custom validator directive?
I wrote a very simple custom validator for an input field:
import { Directive } from '@angular/core';
import { AbstractControl, NG_VALIDATORS } from '@angular/forms';
function numberValidator(c: AbstractControl) {
if (!c.value) return null;
…

Robin Dijkhof
- 18,665
- 11
- 65
- 116
26
votes
3 answers
The pipe 'translate' could not be found , angular2 component testing
I am working on component testing with angular2.
in my html template i use the translate pipe.
This is the code of the test :
import { ComponentFixture, TestBed ,getTestBed} from '@angular/core/testing';
import { By } from…

user3518668
- 359
- 1
- 4
- 14
26
votes
6 answers
Testing - Can't resolve all parameters for (ClassName)
Context
I created an ApiService class to be able to handle our custom API queries, while using our own serializer + other features.
ApiService's constructor signature is:
constructor(metaManager: MetaManager, connector: ApiConnectorService,…

Supamiu
- 8,501
- 7
- 42
- 76
25
votes
1 answer
How to unit test a FormControl in Angular2
My method under test is the following:
/**
* Update properties when the applicant changes the payment term value.
* @return {Mixed} - Either an Array where the first index is a boolean indicating
* that selectedPaymentTerm was set, and…

chap
- 1,860
- 5
- 24
- 39
24
votes
1 answer
How to mock a observable stream while testing angular2 components?
I am writing test cases for angular2 components.
I had created a service which uses observable stream as below:
import {Injectable} from '@angular/core'
import {Subject} from 'rxjs/Subject';
import {User} from…

Bhushan Gadekar
- 13,485
- 21
- 82
- 131
23
votes
4 answers
How to change value of a select box in angular2 unit test?
I have an Angular2 component that contains a select box that looks like
22
votes
2 answers
Angular 2 - test for change in route params
I have a component in angular 2 which responds to changes in the route parameters (the component doesn't reload from scratch because we're not moving out of the main route. Here's the component code:
export class MyComponent{
ngOnInit() {
…

Stu
- 2,426
- 2
- 26
- 42
22
votes
2 answers
Angular2 Testing No provider for LocationStrategy
I am trying to write a test for a Component, but I always get the error: "Error: Error in ./ExpenseOverviewComponent class ExpenseOverviewComponent - inline template:41:8 caused by: No provider for Location Strategy!"
import { ComponentFixture,…

Stefan
- 1,590
- 3
- 18
- 33
20
votes
3 answers
Angular2 unit testing : testing a component's constructor
All is in the title : how can one test what is done in the component's constructor ?
For your information, I am using a service that requires a setting, and I would like to see if the 2 methods that I call in the constructor are called correctly.…
user4676340
20
votes
1 answer
Angular 2 and jQuery - how to test?
I am using Angular-CLI (webpack version) for my Angular 2 project and I also need to use jQuery (sadly. In my case, it's a dependency of Semantic-UI and I am using it for handling menu dropdowns).
The way I am using it:
npm install jquery…

wewo
- 700
- 7
- 16
18
votes
1 answer
How to test if my method throws an observable error in angular2?
I am creating unit-testcases for my angular2 components.
so far test cases are running correctly.
but I am facing issues regarding my asynchronous calls.
For ex. I have following method for creating new user which throws an error if user is already…

Bhushan Gadekar
- 13,485
- 21
- 82
- 131
17
votes
3 answers
How do I check if my element has been focused in a unit test in angular 4?
I have following function to unit test. I have taken element which is text box with view child in component and in testing I need to test whether my text box got focused or not after setTimeout() was called.
@ViewChild('searchInput') searchInput:…

Pranjal
- 463
- 1
- 7
- 20
17
votes
3 answers
How to fix beforeEachProviders (deprecated on RC4)
Ive just upgraded Angular2 from RC3 to RC4 ...
import {
expect, it, iit, xit,
describe, ddescribe, xdescribe,
beforeEach, beforeEachProviders, withProviders,
async, inject
} from '@angular/core/testing';
In my unit test I have the following…

danday74
- 52,471
- 49
- 232
- 283