Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

providedIn: 'root' automatically declare services for testing

Tags:

angular

Maybe it's side effect of this new functionality, but if I have a service as

@Injectable({
  providedIn: 'root'
})
export class MyService {...}

and I have MyComponent which uses this. Now, when I make a test for that component, I simply do and it works!

TestBed.configureTestingModule({
  declarations: [ MyComponent ]
})
.compileComponents();

That means that my service implicitly provided. I think it's very dangerous behaviour for testing. Is it possible to prevent this auto-providing?

like image 762
s-f Avatar asked Jun 12 '18 13:06

s-f


Video Answer


1 Answers

Use Testbed.overrideProvider in your case it will be

TestBed.overrideProvider(MyService, { useValue: MyServiceMock });
like image 67
Bo Vandersteene Avatar answered Sep 19 '22 17:09

Bo Vandersteene