3

Original service

getVersionDataValuesforPopup(Docversion, versionname, Structureweek, docVersionFieldID, versionid, WLTP) {
        return this.http.get(this.apiUrl + 'GetElementPopUpData?docVersion=' + Docversion + '&versionVariant=' + versionname
            + '&structureWeek=' + Structureweek + '&docVersionFieldID=' + docVersionFieldID
            + ' &VersionId=' + versionid + ' &isWLTP=' + WLTP, { withCredentials: true })
            .toPromise().then(responce => <CoCCreateVersionPopupPage[]>responce.json())
            .catch(error => {
                return error;
            });
    }

Calling original service in component

this.createversionservice.getVersionDataValuesforPopup(this.cocviewversiondatapage.docVersion,
            this.cocviewversiondatapage.VersionDescriptions,
            this.cocviewversiondatapage.structWeek, dataitems.DocumentVersionFieldId, this.cocviewversiondatapage.Id,
            this.cocviewversiondatapage.WLTP)
            .then(
            data=> { ...});

Mock service via constructor injection

getVersionDataValuesforPopup(Docversion, versionname, Structureweek, docVersionFieldID, versionid, WLTP) {
        return Observable.of({ Result: {} });
    }

I got an error while test the method

TypeError: this.createversionservice.getVersionDataValuesforPopup(...).then is not a function

I know the reason for the error, I am using Observable in my mock service but the real service has promise, So then callback does not support.Kindly let me know how can write a mock service for a http promise service call.

5
  • 1
    I will suggest some thing like this: return new Promise((resolve) => { resolve(null); }); Commented Feb 7, 2018 at 8:43
  • What I need to import for that?? Commented Feb 7, 2018 at 8:44
  • You need to import nothing. Commented Feb 7, 2018 at 9:08
  • @Nour Yup. that is working for me. Could you please post this as an answer. i'll mark that a correct answer Commented Feb 7, 2018 at 9:50
  • glad to help :) Commented Feb 7, 2018 at 9:54

3 Answers 3

2

What testing framework are you using?

I would create a spy, for example with Jasmine:

spyOn(createversionservice, 'getVersionDataValuesforPopup')

you can then check that your function was called.

If you want to return a promise you can use the .andReturnValue() method, for example:

var promise = Promise.resolve('result');
spyOn(createversionservice, 'getVersionDataValuesforPopup').andReturnValue(promise);

Similar spys will exist in other frameworks

You can then check the method was called (or whatever test you want, for example:

expect(createversionservice.getVersionDataValuesforPopup).toHaveBeenCalled()
Sign up to request clarification or add additional context in comments.

2 Comments

I got this error because I didn't have that function name in my mock service
Solves my problem.
1

Would you please try the following:

return new Promise((resolve) => { resolve({}); });

Comments

1

I have resolved using below code:

Promise.resolve({then: jest.fn()})

1 Comment

This should be the correct answer. It also solves if you get the same error for .catch block. Promise.resolve({then: jest.fn(), catch: jest.fn()})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.