How do you unittest exceptions in Dart?

后端 未结 6 698
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 00:58

Consider a function that does some exception handling based on the arguments passed:

List range(start, stop) {
    i         


        
6条回答
  •  长情又很酷
    2020-12-09 01:40

    I used the following approach:

    First you need to pass in your method as a lambda into the expect function:

    expect(() => method(...), ...)
    

    Secondly you need to use throwsA in combination with isInstanceOf.

    throwsA makes sure that an exception is thrown, and with isInstanceOf you can check if the correct Exception was thrown.

    Example for my unit test:

    expect(() => parser.parse(raw), throwsA(isInstanceOf()));
    

    Hope this helps.

提交回复
热议问题