Andrii Litvinov
Andrii Litvinov

Reputation: 13182

Override pipe when testing component

I am trying to unit test a component that uses custom pipe. I want to provide a fake implementation of transform method for my test.

I have found that it is possible to override components, modules, pipe, etc., but I don't see how it is possible to override the behavior (implementation) of component.

I have tried to provide me custom class as a replacement for pipe and it didn't work:

TestBed
  .configureTestingModule({declarations: [MyPipe]})
  .overridePipe(MDatePipe, {set: MyFakePipe})

I have found similar question on SO How to mock Pipe when testing Component, but suggested solution was to create complete new pipe and provide it to declarations of testing module which is a bit too much in my opinion.

If overridePipe won't allow me to override transform implementation would it be possible to get instance of created MyPipe class and spyOn it?

var pipe = TestBed.get(MyPipe);

didn't work either.

Upvotes: 2

Views: 4564

Answers (1)

Andrii Litvinov
Andrii Litvinov

Reputation: 13182

I haven't found a way to override the behavior of the Pipe so I ended up providing fake service to the real Pipe so that I can mock it's behavior.

Alternative solution would be to create fake pipe implementation for the test as suggested in answer to question How to mock Pipe when testing Component.

Upvotes: 1

Related Questions