NavParams dependency causing "Can't resolve all parameters for NavParams: (?)" while writing Unit test case for the Component


#1

I am getting an error while doing unit test case for a Particular Component which depends upon another 2 components , so in the constructor we have those 2 components

Node : v6.9.1 (AoT)
“ionic-angular”: “2.0.0-rc.2”,
"@angular/common": “2.2.0”,
"@angular/compiler": “2.2.0”,
"@angular/core": “2.2.0”,
"@angular/forms": “2.2.0”,
"@angular/http": “2.2.0”,
"@angular/platform-browser": “2.2.0”,
"@angular/platform-browser-dynamic": “2.2.0”,
"@angular/compiler-cli": “2.2.0”,
"@angular/platform-server": “2.2.0”,

"@types/jasmine": "^2.5.35",
"jasmine-core": "2.5.2",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",

Attached is the Package.json I have.

Below is the error which I am getting

Error: Can’t resolve all parameters for NavParams: (?).

Below is the 2 file contents:
xyz.ts

import {Component, Input, Output, EventEmitter, Inject} from ‘@angular/core’;
import { NavParams } from ‘ionic-angular’;
import {ViewController} from ‘ionic-angular’;

@Component({
templateUrl: ‘xyz.html’
})
export class XYZClass implements OnInit {
constructor(
@Inject(NavParams) protected navParams: NavParams
) {
this.xyz = navParams.data.xyz;
}
}

xyz-spec.ts

import {inject, TestBed} from ‘@angular/core/testing’;
import {Http,BaseRequestOptions,Response,ResponseOptions} from “@angular/http”;

import { Component, OnInit, Inject, Input, NgZone, Output, EventEmitter } from ‘@angular/core’;
import { NavController, NavParams, ActionSheetController, AlertController, Platform, ViewController } from ‘ionic-angular’;

import {XYZClass} from ‘./xyz.ts’;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
XYZClass,
NavParams
]
})
});

describe(‘XYZClass’, () => {

it('is XYZClass',
    inject([XYZClass], (comp: XYZClass) => {
        
        expect(comp).toBeTruthy();
    }));

});

Expected Result :
As described on Angular 2 Docs for Unit testing if anything Is dependent for the the component it needs to be provided in the Providers list in TestBed.configureTestingModule. So the unit test spec should work and should not show error like “Can’t resolve all parameters for NavParams: (?).”


#2

Found any solution for this issue?


#3

I have the same issue, any solution?


#4

Hi @Sathyesh, i’ve solved it creating a MockNavParams. Check my comment on this issue:

Regards,