unit-testing - flutter :单元测试

标签 unit-testing testing dart flutter

我正在尝试测试这个功能:

  setToday(Map filters) {
    if (filters['today'] == false) {
      filters['yesterday'] = false;
      filters['lastWeek'] = false;
      filters['lastMonth'] = false;
      filters['customRange'] = false;
      filters['today'] =  true;
    } else
      filters['today'] = false;
  }

这是测试:

     test("", (){
        Map<String, bool> filters = {
          "today" : false,
          "yesterday" : false,
          "lastWeek" : false,
          "lastMonth" : false,
          "customRange" : false,
        };

        expect(_kpiFilterViewController.setToday(filters), filters["today"] == true);
      });

但结果是:

Expected: <true>
  Actual: <null>

我的错误是什么?

最佳答案

被测函数不返回任何东西,所以调用

_kpiFilterViewController.setToday(filters)

expect 断言将失败。

test("filters[today] value should be true", () {
    //Arrange
    Map<String, bool> filters = {
      "today" : false,
      "yesterday" : false,
      "lastWeek" : false,
      "lastMonth" : false,
      "customRange" : false,
    };
    bool expected = true;

    //Act (call the method under test)
    _kpiFilterViewController.setToday(filters);

    //Assert (verify expected behavior)
    bool actual = filters["today"];
    expect(actual, expected);
});

引用 Flutter: Introduction to unit testing

关于unit-testing - flutter :单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52816759/

相关文章:

Angular 单元测试函数变量

php - 模拟测试对象的公共(public)方法

c# - 预期异常断言

dart - Dart sqljocky问题

android - 无法识别Future函数中的setState

AngularJS 单元测试 element.bind

unit-testing - Controller 测试期间“Failed to load ApplicationContext”错误,并且无法创建Bean

unit-testing - 如何使用新的测试库测试 Dart Polymer 元素?

testing - 功能测试 (QA)

android - 由于缺少初始状态,Firebase 无法处理请求