Can we use both MockProvider and MockInstance to mock a service ? #2621
Answered
by
satanTime
BerrouMoustapha
asked this question in
Q&A
-
Hi All, a simple question regarding ng mocks : Can we use both MockProvider(to mock all functions of the service on beforeEach) and MockInstance(to specify the return of specific function on the test case) to mock a service ? |
Beta Was this translation helpful? Give feedback.
Answered by
satanTime
Jun 2, 2022
Replies: 1 comment 4 replies
-
Yes, it works exactly as you expect: https://codesandbox.io/s/interesting-christian-02b2k5?file=/src/test.spec.ts import { Injectable } from "@angular/core";
import { TestBed } from "@angular/core/testing";
import { MockInstance, MockProvider, MockRender } from "ng-mocks";
@Injectable()
class TargetService {
echo1() {
return this.constructor.name;
}
echo2() {
return this.constructor.name;
}
}
describe("my sandbox", () => {
MockInstance.scope();
beforeEach(() =>
TestBed.configureTestingModule({
providers: [
MockProvider(TargetService, {
echo1: () => "MockProvider"
})
]
}).compileComponents()
);
it("should do something", () => {
MockInstance(TargetService, "echo2", () => "MockInstance");
const { componentInstance: service } = MockRender(TargetService).point;
expect(service.echo1()).toEqual("MockProvider");
expect(service.echo2()).toEqual("MockInstance");
});
}); Just a note, if you use |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
satanTime
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, it works exactly as you expect: https://codesandbox.io/s/interesting-christian-02b2k5?file=/src/test.spec.ts