3

I'm trying to test this function:

  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;
  }

And this is the test:

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

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

But the result is :

Expected: <true>
  Actual: <null>

What is my mistake?

1 Answer 1

3

Function under test does not return anything, so calling

_kpiFilterViewController.setToday(filters)

in the expect assertion will fail.

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);
});

Reference Flutter: Introduction to unit testing

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.