Skip to content
Advertisement

how to fix an error “is not a function” in unit testing angular 11

I saw this error lot of times but any solution for my problem,

my component :

JavaScript

My test :

JavaScript

I don’t understand why I got this error : ” TypeError: this.dialogService.refs.push is not a function”

I try to replace useClass instead of useValue but I had unreachable knowing is angular 11. I did lot of research but I found anything.

thanks to all for help

Advertisement

Answer

When you do:

JavaScript

You’re saying that there is a refs method on DialogService that I would like to mock but refs is not a method, it’s an instance variable.

To fix it, I would do this:

JavaScript

Now we attached a refs property with an empty array and hopefully you shouldn’t see that error anymore. We assigned an empty array so .push will work.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement