1

I'm using Mockito in flutter application and want to add some test cases:

this run failed while the actual result is the same as expected result, here's my test file:

@GenerateMocks([ResumeParserDataSource])
void main() {
  late MockResumeParserDataSource resumeParserDataSource;
  late ResumeParserController resumeParserController;
  late ResumeParserRepo resumeParserRepo;

  setUp(() async {
    Get.reset();
    resumeParserDataSource = MockResumeParserDataSource();
    resumeParserRepo =
        Get.put(ResumeParserRepo(dataSource: resumeParserDataSource));
    resumeParserController = Get.put(ResumeParserController());
  });

  group('Upload resume that uploaded into API:\n', () {
    test(
        '~succeed state~ api get response and parsed it into model JSON "ParsedDetailsResume"',
        () async {
      when(resumeParserRepo
              .getResumeDetailsAfterParsing(ResumeParserTestUtil.fileName))
          .thenAnswer((_) async => Right(
              ResumeParserTestUtil.parsedResumeDetailsFunction(
                  ResumeParserTestUtil.parsedResumeDetailsModel1)));
      await resumeParserController
          .parseResumeToAPI(ResumeParserTestUtil.fileName);
      await waitForAPIResponse();
      expect(
          resumeParserController.parsedResume.value,
          some(ResumeParserTestUtil.parsedResumeDetailsFunction(
              ResumeParserTestUtil.parsedResumeDetailsModel1)));
      expect(resumeParserController.error.value, none());
    });
  });
}

here's my ResumeParserController:


class ResumeParserController extends GetxController {
  static ResumeParserController get to => Get.find();
  final resumeParserRepo = ResumeParserRepo.to;
  Rx<Option<ParsedResumeDetails>> parsedResume =
      Rx<Option<ParsedResumeDetails>>(none());

  Future<Either<DioError, ParsedResumeDetails>> parseResumeToAPI(
      String fileName,
      {User? user}) async {
    var result = await resumeParserRepo.getResumeDetailsAfterParsing(
      fileName,
      user: user,
    );
    return result.fold(
      (l) => Left(l),
      (newParsedResume) async {
        error.value = none();
        parsedResume.value = some(newParsedResume);
        update();
        return Right(newParsedResume);
      },
    );
  }
}

and here's my json model in ResumeParserTestUtil:

class ResumeParserTestUtil {

  static String fileName = "12989801ahsduiabsdui9123ajd.pdf";

  static ParsedResumeDetails parsedResumeDetailsFunction(
      Map<String, dynamic> response,
      {User? user}) {
    var userSkills = response['data']['userSkills'] == null
        ? [PublicUserSkill.empty()]
        : List.from(response['data']['userSkills'])
            .map((e) => PublicUserSkill.fromJson(e["skill"]))
            .toList();
    var userEducations = response['data']['educations'] == null
        ? [EducationParsed.empty()]
        : List.from(response['data']['educations'])
            .map((e) => EducationParsed.fromJson(e))
            .toList();

    var experiences = response['data']['experiences'] == null
        ? [ExperiencesParsed.empty()]
        : List.from(response['data']['experiences'])
            .map((e) => ExperiencesParsed.fromJson(e))
            .toList();
    return ParsedResumeDetails(
      user: user,
      userSkills:
          userSkills.map(mapPublicUserSkillToSkillWithUserSkill).toList(),
      userEducation: userEducations,
      userWorkExperience: experiences,
    );
  }

  static Map<String, dynamic> parsedResumeDetailsModel1 = {
    "data": {
      "experiences": [
        {
          "startDate": null,
          "endDate": null,
          "isPresent": false,
          "organization": "Unnamed company",
          "type": "WORK"
        }
      ],
      "userSkills": [
        {
          "skill": {
            "id": "7963a547-3980-47d6-8ffc-c9e311152d9b",
            "name": "Bachelors Degree > Bachelors Degree Economics",
            "popularity": "0",
            "flagRecount": false,
            "createdAt": null,
            "updatedAt": null,
          },
        },
        {
          "skill": {
            "id": "05516ee5-1706-4296-aaa3-f3386767dec6",
            "name":
                "Bachelors Degree > Bachelors Degree Information Engineering and Media",
            "popularity": "0",
            "flagRecount": false,
            "createdAt": null,
            "updatedAt": null,
          }
        },
        {
          "skill": {
            "id": "0baaa115-a770-42f5-ad54-291ffa42a11c",
            "name": "Bachelors Degree > Bachelors Degree Literature",
            "popularity": "0",
            "flagRecount": false,
            "createdAt": null,
            "updatedAt": null
          }
        },
        {
          "skill": {
            "id": "7a4ca05f-8d75-44c5-b012-5a0298160903",
            "name": "Bachelors Degree > Bachelors Degree Mathematics",
            "popularity": "0",
            "flagRecount": false,
            "createdAt": null,
            "updatedAt": null,
          },
        },
        {
          "skill": {
            "id": "b29215d5-c3cf-4b62-82df-2ee5db1c160c",
            "name": "Bachelors Degree > Bachelors Degree history",
            "popularity": "0",
            "flagRecount": false,
            "createdAt": null,
            "updatedAt": null
          }
        }
      ],
      "educations": [
        {
          "startDate": null,
          "endDate": null,
          "type": "EDUCATION",
          "isPresent": true,
          "study": {
            "id": "572502d1-fb5c-4876-8985-2c4278b98a8b",
            "name":
                "Information Engineering and Media, history, Economics, Literature, Mathematics",
            "createdAt": "",
            "updatedAt": ""
          },
          "school": {
            "id": "f54c85dd-f51e-4715-a393-dbcea6edbed7",
            "name": "Adamson University",
            "domain": "adamson.edu.ph",
            "website": "http://www.adamson.edu.ph/",
            "state": null,
            "type": null,
            "CountryCode": "PH",
            "createdAt": null,
            "updatedAt": null
          },
          "degree": {
            "id": "63dd0edf-9193-4ac0-b693-ae561873e4a3",
            "name": "Bachelor",
            "createdAt": "",
            "updatedAt": ""
          }
        },
        {
          "startDate": null,
          "endDate": null,
          "type": "EDUCATION",
          "isPresent": true,
          "school": {
            "id": "1dedeca8-154b-4e8f-a867-d359616d996c",
            "name": "Adipala Junior High School",
            "domain": null,
            "website": null,
            "state": null,
            "type": null,
            "CountryCode": null,
            "createdAt": "",
            "updatedAt": null
          }
        },
        {
          "startDate": null,
          "endDate": null,
          "type": "EDUCATION",
          "isPresent": true,
          "school": {
            "id": "38420130-dac4-4a3c-a1bc-cea4777054c1",
            "name": "Unnamed school",
            "domain": null,
            "website": null,
            "state": null,
            "type": null,
            "CountryCode": null,
            "createdAt": null,
            "updatedAt": null
          }
        }
      ]
    }
  };
}

and this was the result:

Expected: Some:<Some(ParsedResumeDetails([EducationParsed(true, null, null, null, EDUCATION), EducationParsed(true, null, null, null, EDUCATION), EducationParsed(true, null, null, null, EDUCATION)], [ExperiencesParsed(false, null, Unnamed company, null, WORK)], [Instance of 'SkillWithUserSkill', Instance of 'SkillWithUserSkill', Instance of 'SkillWithUserSkill', Instance of 'SkillWithUserSkill', Instance of 'SkillWithUserSkill'], null))>

Actual: Some:<Some(ParsedResumeDetails([EducationParsed(true, null, null, null, EDUCATION), EducationParsed(true, null, null, null, EDUCATION), EducationParsed(true, null, null, null, EDUCATION)], [ExperiencesParsed(false, null, Unnamed company, null, WORK)], [Instance of 'SkillWithUserSkill', Instance of 'SkillWithUserSkill', Instance of 'SkillWithUserSkill', Instance of 'SkillWithUserSkill', Instance of 'SkillWithUserSkill'], null))>

package:test_api

✖ Upload resume that uploaded into API: ~succeed state~ api get response and parsed it into model JSON "ParsedDetailsResume"

Exited (1)

Any thoughts on this? Thanks!

1
  • 1
    Perhaps the second expect is throwing an error that is garbling the report. I would try first to take out the second expect(), then wrap the first one in a try catch to see if something is throwing an error that's not being caught. Otherwise, based on what you provided, nothing is obviously wrong. Commented Dec 10, 2021 at 15:49

1 Answer 1

1

Ran into same issue trying test async call, where it was failing because I set expected value wrapped in a Future.value()

Solution in this case is to :

  1. extract the value from the async call first with await
  2. then do the check on raw value, without any notion of Future<>

eg:

test('test SomeService()', () async {

  final actual = await someService.getSomeList();

  expect(actual, []);
});
Sign up to request clarification or add additional context in comments.

Comments

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.