【问题标题】:Angular 4 Jasmine Actual is not a FunctionAngular 4 Jasmine Actual 不是函数
【发布时间】:2017-09-28 01:46:29
【问题描述】:

我正在做expect(ClassUnderTest.someMethod(withSomeParams)).toThrow(),我得到:-

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

我不明白使用示例。

我尝试了expect(() =&gt; ClassUnderTest.someMethod(withSomeParams)).toThrow() 我得到了Expected function to throw an exception.。并尝试过:-

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

我得到了Error: 1 periodic timer(s) still in the queue.

我不明白如何为抛出错误的时间编写这个期望。

【问题讨论】:

    标签: angular testing jasmine karma-jasmine


    【解决方案1】:

    您需要将函数本身传递给expect。按照您的方式,您将传递ClassUnderTest.someMethod(withSomeParams)结果

    您实际上在expect(() =&gt; ClassUnderTest.someMethod(withSomeParams)).toThrow() 中做得正确。该错误是由于您的实现中的实际错误,或者是因为this 与箭头函数绑定。

    要解决此问题,您可以尝试:

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

    或:

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

    参见this StackOverflow postJasmine docs 中的.toThrow 部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多