This repository was archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
This repository was archived by the owner on Jun 22, 2024. It is now read-only.
Generated test assigns the 'null' value to non-nullable value #2
Copy link
Copy link
Open
Description
I have a model 'product.dart'. I wanted to test that product using welltested.
here is the model code.
import 'package:json_annotation/json_annotation.dart';
import 'package:welltested/welltested.dart';
part 'product.g.dart';
@JsonSerializable()
@Welltested()
class Product {
final int id;
final String title;
final num price;
final String description;
final String image;
final String category;
final Rating rating;
Product(
{required this.category,
required this.description,
required this.id,
required this.image,
required this.price,
required this.title,
required this.rating});
factory Product.fromJson(Map<String, dynamic> json) =>
_$ProductFromJson(json);
Map<String, dynamic> toJson() => _$ProductToJson(this);
}
@JsonSerializable()
class Rating {
final num rating;
final int count;
Rating({
required this.count,
required this.rating,
});
factory Rating.fromJson(Map<String, dynamic> json) => _$RatingFromJson(json);
Map<String, dynamic> toJson() => _$RatingToJson(this);
}
Two test files were generated by welltested ai.
Here is the code for each of the files :
toJson.welltested_test.dart
import 'toJson.welltested_test.mocks.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:api_integrations/product.dart';
// Generate mocks for Rating class
@GenerateMocks([Rating])
void main() {
group('Product toJson method', () {
test('should return a valid JSON map', () {
// Arrange
final ratingMock = MockRating();
when(ratingMock.rating).thenReturn(4.5);
when(ratingMock.count).thenReturn(100);
final product = Product(
id: 1,
title: 'Test Product',
price: 9.99,
description: 'This is a test product',
image: 'test_image.jpg',
category: 'Test Category',
rating: ratingMock,
);
// Act
final result = product.toJson();
// Assert
expect(result, isA<Map<String, dynamic>>());
expect(result['id'], equals(1));
expect(result['title'], equals('Test Product'));
expect(result['price'], equals(9.99));
expect(result['description'], equals('This is a test product'));
expect(result['image'], equals('test_image.jpg'));
expect(result['category'], equals('Test Category'));
expect(result['rating'], isA<Map<String, dynamic>>());
expect(result['rating']['rating'], equals(4.5));
expect(result['rating']['count'], equals(100));
});
test('should return a JSON map with null values', () {
// Arrange
final product = Product(
id: 1,
title: 'Test Product',
price: null,
description: null,
image: null,
category: null,
rating: null,
);
// Act
final result = product.toJson();
// Assert
expect(result, isA<Map<String, dynamic>>());
expect(result['id'], equals(1));
expect(result['title'], equals('Test Product'));
expect(result['price'], isNull);
expect(result['description'], isNull);
expect(result['image'], isNull);
expect(result['category'], isNull);
expect(result['rating'], isNull);
});
test('should throw an exception if id is null', () {
// Arrange
final ratingMock = MockRating();
when(ratingMock.rating).thenReturn(4.5);
when(ratingMock.count).thenReturn(100);
final product = Product(
id: null,
title: 'Test Product',
price: 9.99,
description: 'This is a test product',
image: 'test_image.jpg',
category: 'Test Category',
rating: ratingMock,
);
// Act & Assert
expect(
() => product.toJson(), throwsA(isA<JsonUnsupportedObjectError>()));
});
});
}
toJson.welltested_test.mocks.dart
// Mocks generated by Mockito 5.4.2 from annotations
// in api_integrations/test/product/0.wt.dart.
// Do not manually edit this file.
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:api_integrations/product.dart' as _i2;
import 'package:mockito/mockito.dart' as _i1;
// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class
/// A class which mocks [Rating].
///
/// See the documentation for Mockito's code generation for more information.
class MockRating extends _i1.Mock implements _i2.Rating {
MockRating() {
_i1.throwOnMissingStub(this);
}
@override
num get rating => (super.noSuchMethod(
Invocation.getter(#rating),
returnValue: 0,
) as num);
@override
int get count => (super.noSuchMethod(
Invocation.getter(#count),
returnValue: 0,
) as int);
@override
Map<String, dynamic> toJson() => (super.noSuchMethod(
Invocation.method(
#toJson,
[],
),
returnValue: <String, dynamic>{},
) as Map<String, dynamic>);
}
My pubspec.yaml file
name: api_integrations
description: A new Flutter project.
publish_to: 'none'
version: 0.1.0
environment:
sdk: '>=3.0.0-408.0.dev <4.0.0'
dependencies:
flutter:
sdk: flutter
http: ^0.13.6
json_serializable: ^6.6.2
mockito: ^5.4.2
welltested: ^1.0.4
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
build_runner:
flutter:
uses-material-design: true
Thank you !
Metadata
Metadata
Assignees
Labels
No labels