user223364
user223364

Reputation: 529

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.

Upvotes: 4

Views: 2351

Answers (1)

Harrison Cole
Harrison Cole

Reputation: 31

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.

Upvotes: 2

Related Questions