I'm trying to learn flutter TDD and I would like to make a code that should return a valid BusinessCardModel from json file that contains information about business card(s).
but when I try to run this test:
import 'dart:convert';
import 'package:cardatyapp/core/data/models/businessCard_model.dart';
import 'package:cardatyapp/core/domain/entities/businessCard.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../../fixtures/fixtures_reader.dart';
void main() {
final tbusinessCardModel = BusinessCardModel(
id: '01',
jobTitle: 'fullstack developer',
jobDetails: 'creating fontend, backend & mobile applications',
organization: 'freelance',
);
test('sould be a subclass of the Card entity', () {
//assert
expect(tbusinessCardModel, isA<BusinessCard>());
});
test(
'should return a valid BusinessCardModel when json file contains BusinessCard',
() async {
//arrange
final Map<String, dynamic> jsonMap = json.decode(readFixture('card.json'));
//act
final result = BusinessCardModel.fromJson(jsonMap);
//assert
expect(result, tbusinessCardModel);
});
}
I've got this error! which i couldn't understand?
ERROR: Expected: BusinessCardModel:<BusinessCardModel>
Actual: BusinessCardModel:<BusinessCardModel>
Here are code snippets:
card.json file:
{
"uuid": "recqPz5M7tQTYesc3",
"fav_by_user": [
"reclZ8MXHwYKSXgNz"
],
"created_by_user": [
"recQ0XTSbKoezQCmQ"
],
"job_title": "Account Executive",
"id_card": "1",
"organization": "Buzzshare",
"job_details": "In hac habitasse platea dictumst. Etiam faucibus cursus urna. Ut tellus.\n\nNulla ut erat id mauris vulputate elementum. Nullam varius. Nulla facilisi.",
"createdTime": "2020-10-02T08:41:13.000Z"
}
Business card entity:
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
class BusinessCard extends Equatable {
final String id;
final String jobTitle;
final String jobDetails;
final String organization;
BusinessCard({
this.id,
@required this.jobTitle,
@required this.jobDetails,
@required this.organization,
});
@override
List<Object> get props => [
id,
jobTitle,
jobDetails,
organization,
];
}
Business card Model (based on the precedent entity) :
import 'package:meta/meta.dart';
import '../../domain/entities/businessCard.dart';
class BusinessCardModel extends BusinessCard {
BusinessCardModel({
String id,
@required String jobTitle,
@required String jobDetails,
@required String organization,
}) : super(
id: id,
jobTitle: jobTitle,
jobDetails: jobDetails,
organization: organization,
);
factory BusinessCardModel.fromJson(Map<String, dynamic> jsonMap) {
return BusinessCardModel(
id: jsonMap['id_card'],
jobTitle: jsonMap['jobTitle'],
jobDetails: jsonMap['jobDetails'],
organization: jsonMap['organization'],
);
}
}
To read the json file I'm using a fixtures reader:
import 'dart:io';
String readFixture(String fileName) =>
File('test/fixtures/$fileName').readAsStringSync();
Please help me to understand & solve that error!
Your
BusinessCardModel
is extendingBusinessCard
which extendsEquatable
with all members as props. As a result when you're comparing two different instance of model with different values for members, you will get false which you're doing now! The model you're expecting'sid
is'01'
and the model you're gonna get runningBusinessCardModel.fromJson(jsonMap)
has'1'
asid
. You should change yourtbusinessCardModel
to match the fixture you have defined.