ViewController testing error: Cannot read property 'subscribe' of undefined

Hi everybody!

I am triying to test a Page with ViewController injected but when I inject it and test it with ViewController mock, it’s giving me this error (I did nothing more) Error: Error in ./ProductAddPage class ProductAddPage - inline template:7:0 caused by: Cannot read property ‘subscribe’ of undefined

This is the code for ProductAddPage:

export class ProductAddPage {
..
	constructor(private navParams: NavParams, private productService: ProductService, private camera: Camera, private navCtrl: NavController,
		private viewCtrl: ViewController, private translate: TranslateService, private alertCtrl: AlertController) {
		this.product = new Product();
		this.product.codeBar = this.navParams.get('productCodeBar');
	}
..

AddProductTest

..
export class ViewControllerMock {

    public index: number = 0;

    public remove(startIndex: number): any { return {} };
    public _setHeader(): any { return {} };
    public _setIONContent(): any { return {} };
    public _setIONContentRef(): any { return {} };
    public _setNavbar(): any { return {} };
}

describe('ProductAddPage Component', () => {

    let fixture;
    let component: ProductAddPage;

    beforeEach(() => {

        // It will return 111 value when NavParams is called
        NavParamsMock.setParams('productCodeBar', 111);

        TestBed.configureTestingModule({
            declarations: [ProductAddPage],
            imports: [
                IonicModule.forRoot(ProductAddPage),
                TranslateModule.forRoot()
            ],
            providers: [
                { provide: NavParams, useClass: NavParamsMock },
                { provide: ProductService, useClass: ProductServiceMock },
                { provide: ViewController, useClass: ViewControllerMock },
                NavController,
                TranslateService,
                Camera
            ]
        });

        fixture = TestBed.createComponent(ProductAddPage);
        component = fixture.componentInstance;

    });

    it('should instantiate component', () => {
        expect(component instanceof ProductAddPage).toBe(true, 'Should create ProductAddPage');
    });
..

EDIT:

Ok, I had to put ViewControllerMock like this and now is working well:

export class ViewControllerMock {

    public index: number = 0;

    public readReady = {
        subscribe() {

        }
    };
    public writeReady = {
        subscribe() {

        }
    };

    dismiss() {
        console.log('View Controller Dismiss Called');
    }

    public remove(startIndex: number): any { return {} };
    public _setHeader(): any { return {} };
    public _setIONContent(): any { return {} };
    public _setIONContentRef(): any { return {} };
    public _setNavbar(): any { return {} };
}
2 Likes