lundi 11 décembre 2017

Mock a class used as property of a service

I am actually developing an Ionic application. In this context, I implemented an AlertService class which has the two following properties:

messageAlert: Alert;
errorAlert: Alert;

The Alert class is the one of the Ionic framework so I don't have any control on it. My idea was to mock the Alert class by a homemade mock like the following class:

class AlertMock {
    opts:       Object;
    presented:  boolean;

    constructor(opts) {
        this.opts       = opts;
        this.presented  = false;
    }

    present() {
        this.presented  = true;
    }

    dismiss() {
        this.presented  = false;
    }
}

It will allow me to simply test the alert state by checking the value of the presented property.

But how can I indicate to my AlertService that it two Alert properties are of type AlertMock instead? I don't know if it is even possible. But it seems to me quite basic to be able to mock class like that. Every topic I found talk about mocking service. With DI, it's very simple to replace a type by another, but is there any mechanism to do it with simple class like Alert of the Ionic framework?

Aucun commentaire:

Enregistrer un commentaire