简体   繁体   English

测试带有笑话问题的 ES6 class 方法

[英]Testing ES6 class method with jest problem

My class -我的 class -

class Course{
  public getFormattedTime(){
    // logic to parse time and return in appropriate format
    // return string
  }
}

According to jest documentation https://jestjs.io/docs/en/es6-class-mocks#automatic-mock , replaces all of its methods with mock functions that always return undefined .根据 jest 文档https://jestjs.io/docs/en/es6-class-mocks#automatic-mockreplaces all of its methods with mock functions that always return undefined So my question is if we're changing all the method return values to undefined then what else there's left to test?所以我的问题是,如果我们将所有方法返回值更改为未定义,那么还有什么要测试的呢? I do not want to test how many time this method get's called, I just want to test the actual logic inside the method.我不想测试这个方法被调用了多少次,我只想测试方法内部的实际逻辑。 Am I missing something here我在这里错过了什么吗

The goal of testing is to test only and only the function you are targeting.测试的目标是仅测试您的目标 function。 If the function is calling another one, then the second function is supposed to be mocked.如果 function 正在调用另一个,那么第二个 function 应该被模拟。 The automatic mocking will just create a fake class and each method will return undefined.自动 mocking 只会创建一个假的 class 并且每个方法都将返回未定义。 If you want your methods to return specific values for the testing purpose, then you should have a look on manual mocking.如果您希望您的方法为测试目的返回特定值,那么您应该查看手册 mocking。 https://jestjs.io/docs/en/es6-class-mocks#manual-mock https://jestjs.io/docs/en/es6-class-mocks#manual-mock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM