How do you unittest exceptions in Dart?

后端 未结 6 697
没有蜡笔的小新
没有蜡笔的小新 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

    An exception is checked using throwsA with TypeMatcher.

    Note: isInstanceOf is now deprecated.

    List range(start, stop) {
        if (start >= stop) {
          throw new ArgumentError("start must be less than stop");
        }
        // remainder of function
    }
    
    
    test("check argument error", () {
      expect(() => range(1, 2), throwsA(TypeMatcher()));
    }); 
    

提交回复
热议问题