Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 Jasmine Actual is not a Function

I'm doing expect(ClassUnderTest.someMethod(withSomeParams)).toThrow() and I'm getting:-

Error: <toThrow> : Actual is not a Function
Usage: expect(function() {<expectation>}).toThrow(<ErrorConstructor>, <message>)

and I don't understand the usage example.

I tried expect(() => ClassUnderTest.someMethod(withSomeParams)).toThrow() I got Expected function to throw an exception.. And tried:-

ClassUnderTest.someMethod(withSomeParams)
              .subscribe( res => res,
                          err => console.log(err))

and I got Error: 1 periodic timer(s) still in the queue.

I don't understand how to write this expectation for when the error is thrown.

like image 557
user223364 Avatar asked Sep 08 '25 13:09

user223364


1 Answers

You need to pass the function itself into expect. The way you have it, you're passing the result of ClassUnderTest.someMethod(withSomeParams) in.

You are actually doing it correctly in expect(() => ClassUnderTest.someMethod(withSomeParams)).toThrow(). The error is either due to an actual error in your implementation, or because of this binding with arrow functions.

To fix this, you can try:

expect(function () { ClassUnderTest.someMethod(withSomeParams) };).toThrow()

or:

expect(ClassUnderTest.someMethod.bind(null, withSomeParams)).toThrow()

See this StackOverflow post and the .toThrow section in the Jasmine docs.

like image 56
Harrison Cole Avatar answered Sep 10 '25 05:09

Harrison Cole