Skip to content

Can we use both MockProvider and MockInstance to mock a service ? #2621

Answered by satanTime
BerrouMoustapha asked this question in Q&A
Discussion options

You must be logged in to vote

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",

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@BerrouMoustapha
Comment options

@satanTime
Comment options

@BerrouMoustapha
Comment options

@satanTime
Comment options

Answer selected by satanTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants