Skip to content

Commit

Permalink
move mocks to factory helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
rmanguinho committed Oct 28, 2020
1 parent 93e1a0b commit a3ce74c
Show file tree
Hide file tree
Showing 32 changed files with 304 additions and 321 deletions.
2 changes: 1 addition & 1 deletion lib/data/models/local_survey_answer_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LocalSurveyAnswerModel {
return LocalSurveyAnswerModel(
image: json['image'],
answer: json['answer'],
isCurrentAnswer: bool.fromEnvironment(json['isCurrentAnswer']),
isCurrentAnswer: json['isCurrentAnswer'].toLowerCase() == 'true',
percent: int.parse(json['percent'])
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/data/models/local_survey_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LocalSurveyModel {
id: json['id'],
question: json['question'],
date: DateTime.parse(json['date']),
didAnswer: bool.fromEnvironment(json['didAnswer']),
didAnswer: json['didAnswer'].toLowerCase() == 'true',
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/domain/entities/survey_answer_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SurveyAnswerEntity extends Equatable {
final bool isCurrentAnswer;
final int percent;

List get props => ['image', 'answer', 'isCurrentAnswer', 'percent'];
List get props => [image, answer, isCurrentAnswer, percent];

SurveyAnswerEntity({
this.image,
Expand Down
2 changes: 1 addition & 1 deletion lib/domain/entities/survey_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SurveyEntity extends Equatable {
final DateTime dateTime;
final bool didAnswer;

List get props => ['id', 'question', 'dateTime', 'didAnswer'];
List get props => [id, question, dateTime, didAnswer];

SurveyEntity({
@required this.id,
Expand Down
2 changes: 1 addition & 1 deletion lib/domain/entities/survey_result_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SurveyResultEntity extends Equatable {
final String question;
final List<SurveyAnswerEntity> answers;

List get props => ['surveyId', 'question', 'answers'];
List get props => [surveyId, question, answers];

SurveyResultEntity({
@required this.surveyId,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/survey_result/survey_answer_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SurveyAnswerViewModel extends Equatable {
final bool isCurrentAnswer;
final String percent;

List get props => ['image', 'answer', 'isCurrentAnswer', 'percent'];
List get props => [image, answer, isCurrentAnswer, percent];

SurveyAnswerViewModel({
this.image,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/survey_result/survey_result_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SurveyResultViewModel extends Equatable {
final String question;
final List<SurveyAnswerViewModel> answers;

List get props => ['surveyId', 'question', 'answers'];
List get props => [surveyId, question, answers];

SurveyResultViewModel({
@required this.surveyId,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/surveys/survey_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SurveyViewModel extends Equatable {
final String date;
final bool didAnswer;

List get props => ['id', 'question', 'date', 'didAnswer'];
List get props => [id, question, date, didAnswer];

SurveyViewModel({
@required this.id,
Expand Down
24 changes: 10 additions & 14 deletions test/data/usecases/add_account/remote_add_account_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,36 @@ import 'package:ForDev/domain/usecases/usecases.dart';
import 'package:ForDev/data/http/http.dart';
import 'package:ForDev/data/usecases/usecases.dart';

import '../../../mocks/mocks.dart';

class HttpClientSpy extends Mock implements HttpClient {}

void main() {
RemoteAddAccount sut;
HttpClientSpy httpClient;
String url;
AddAccountParams params;

Map mockValidData() => {'accessToken': faker.guid.guid(), 'name': faker.person.name()};
Map apiResult;

PostExpectation mockRequest() => when(httpClient.request(
url: anyNamed('url'),
method: anyNamed('method'),
body: anyNamed('body')
));

void mockHttpData(Map data) => mockRequest().thenAnswer((_) async => data);
void mockHttpData(Map data) {
apiResult = data;
mockRequest().thenAnswer((_) async => data);
}

void mockHttpError(HttpError error) => mockRequest().thenThrow(error);

setUp(() {
httpClient = HttpClientSpy();
url = faker.internet.httpUrl();
sut = RemoteAddAccount(httpClient: httpClient, url: url);
params = AddAccountParams(
name: faker.person.name(),
email: faker.internet.email(),
password: faker.internet.password(),
passwordConfirmation: faker.internet.password()
);
mockHttpData(mockValidData());
params = FakeParamsFactory.makeAddAccount();
mockHttpData(FakeAccountFactory.makeApiJson());
});

test('Should call HttpClient with correct values', () async {
Expand Down Expand Up @@ -88,12 +87,9 @@ void main() {
});

test('Should return an Account if HttpClient returns 200', () async {
final validData = mockValidData();
mockHttpData(validData);

final account = await sut.add(params);

expect(account.token, validData['accessToken']);
expect(account.token, apiResult['accessToken']);
});

test('Should throw UnexpectedError if HttpClient returns 200 with invalid data', () async {
Expand Down
19 changes: 10 additions & 9 deletions test/data/usecases/authentication/remote_authentication_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,36 @@ import 'package:ForDev/domain/usecases/usecases.dart';
import 'package:ForDev/data/http/http.dart';
import 'package:ForDev/data/usecases/usecases.dart';

import '../../../mocks/mocks.dart';

class HttpClientSpy extends Mock implements HttpClient {}

void main() {
RemoteAuthentication sut;
HttpClientSpy httpClient;
String url;
AuthenticationParams params;

Map mockValidData() => {'accessToken': faker.guid.guid(), 'name': faker.person.name()};
Map apiResult;

PostExpectation mockRequest() => when(httpClient.request(
url: anyNamed('url'),
method: anyNamed('method'),
body: anyNamed('body')
));

void mockHttpData(Map data) => mockRequest().thenAnswer((_) async => data);
void mockHttpData(Map data) {
apiResult = data;
mockRequest().thenAnswer((_) async => data);
}

void mockHttpError(HttpError error) => mockRequest().thenThrow(error);

setUp(() {
httpClient = HttpClientSpy();
url = faker.internet.httpUrl();
sut = RemoteAuthentication(httpClient: httpClient, url: url);
params = AuthenticationParams(email: faker.internet.email(), secret: faker.internet.password());
mockHttpData(mockValidData());
params = FakeParamsFactory.makeAuthentication();
mockHttpData(FakeAccountFactory.makeApiJson());
});

test('Should call HttpClient with correct values', () async {
Expand Down Expand Up @@ -78,12 +82,9 @@ void main() {
});

test('Should return an Account if HttpClient returns 200', () async {
final validData = mockValidData();
mockHttpData(validData);

final account = await sut.auth(params);

expect(account.token, validData['accessToken']);
expect(account.token, apiResult['accessToken']);
});

test('Should throw UnexpectedError if HttpClient returns 200 with invalid data', () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:ForDev/domain/helpers/helpers.dart';
import 'package:ForDev/data/cache/cache.dart';
import 'package:ForDev/data/usecases/usecases.dart';

import '../../../mocks/mocks.dart';

class CacheStorageSpy extends Mock implements CacheStorage {}

void main() {
Expand All @@ -16,21 +18,6 @@ void main() {
Map data;
String surveyId;

Map mockValidData() => {
'surveyId': faker.guid.guid(),
'question': faker.lorem.sentence(),
'answers': [{
'image': faker.internet.httpUrl(),
'answer': faker.lorem.sentence(),
'isCurrentAnswer': 'true',
'percent': '40'
}, {
'answer': faker.lorem.sentence(),
'isCurrentAnswer': 'false',
'percent': '60'
}],
};

PostExpectation mockFetchCall() => when(cacheStorage.fetch(any));

void mockFetch(Map json) {
Expand All @@ -44,7 +31,7 @@ void main() {
surveyId = faker.guid.guid();
cacheStorage = CacheStorageSpy();
sut = LocalLoadSurveyResult(cacheStorage: cacheStorage);
mockFetch(mockValidData());
mockFetch(FakeSurveyResultFactory.makeCacheJson());
});

test('Should call cacheStorage with correct key', () async {
Expand Down Expand Up @@ -92,26 +79,15 @@ void main() {
});

test('Should throw UnexpectedError if cache is isvalid', () async {
mockFetch({
'surveyId': faker.guid.guid(),
'question': faker.lorem.sentence(),
'answers': [{
'image': faker.internet.httpUrl(),
'answer': faker.lorem.sentence(),
'isCurrentAnswer': 'invalid bool',
'percent': 'invalid int'
}],
});
mockFetch(FakeSurveyResultFactory.makeInvalidCacheJson());

final future = sut.loadBySurvey(surveyId: surveyId);

expect(future, throwsA(DomainError.unexpected));
});

test('Should throw UnexpectedError if cache is incomplete', () async {
mockFetch({
'surveyId': faker.guid.guid()
});
mockFetch(FakeSurveyResultFactory.makeIncompleteCacheJson());

final future = sut.loadBySurvey(surveyId: surveyId);

Expand All @@ -133,21 +109,6 @@ void main() {
Map data;
String surveyId;

Map mockValidData() => {
'surveyId': faker.guid.guid(),
'question': faker.lorem.sentence(),
'answers': [{
'image': faker.internet.httpUrl(),
'answer': faker.lorem.sentence(),
'isCurrentAnswer': 'true',
'percent': '40'
}, {
'answer': faker.lorem.sentence(),
'isCurrentAnswer': 'false',
'percent': '60'
}],
};

PostExpectation mockFetchCall() => when(cacheStorage.fetch(any));

void mockFetch(Map json) {
Expand All @@ -161,7 +122,7 @@ void main() {
surveyId = faker.guid.guid();
cacheStorage = CacheStorageSpy();
sut = LocalLoadSurveyResult(cacheStorage: cacheStorage);
mockFetch(mockValidData());
mockFetch(FakeSurveyResultFactory.makeCacheJson());
});

test('Should call cacheStorage with correct key', () async {
Expand All @@ -171,26 +132,15 @@ void main() {
});

test('Should delete cache if it is invalid', () async {
mockFetch({
'surveyId': faker.guid.guid(),
'question': faker.lorem.sentence(),
'answers': [{
'image': faker.internet.httpUrl(),
'answer': faker.lorem.sentence(),
'isCurrentAnswer': 'invalid bool',
'percent': 'invalid int'
}],
});
mockFetch(FakeSurveyResultFactory.makeInvalidCacheJson());

await sut.validate(surveyId);

verify(cacheStorage.delete('survey_result/$surveyId')).called(1);
});

test('Should delete cache if it is incomplete', () async {
mockFetch({
'surveyId': faker.guid.guid()
});
mockFetch(FakeSurveyResultFactory.makeIncompleteCacheJson());

await sut.validate(surveyId);

Expand All @@ -215,28 +165,10 @@ void main() {

void mockSaveError() => mockSaveCall().thenThrow(Exception());

SurveyResultEntity mockSurveyResult() => SurveyResultEntity(
surveyId: faker.guid.guid(),
question: faker.lorem.sentence(),
answers: [
SurveyAnswerEntity(
image: faker.internet.httpUrl(),
answer: faker.lorem.sentence(),
isCurrentAnswer: true,
percent: 40
),
SurveyAnswerEntity(
answer: faker.lorem.sentence(),
isCurrentAnswer: false,
percent: 60
)
]
);

setUp(() {
cacheStorage = CacheStorageSpy();
sut = LocalLoadSurveyResult(cacheStorage: cacheStorage);
surveyResult = mockSurveyResult();
surveyResult = FakeSurveyResultFactory.makeEntity();
});

test('Should call cacheStorage with correct values', () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:ForDev/domain/helpers/helpers.dart';
import 'package:ForDev/data/http/http.dart';
import 'package:ForDev/data/usecases/usecases.dart';

import '../../../mocks/mocks.dart';

class HttpClientSpy extends Mock implements HttpClient {}

void main() {
Expand All @@ -15,24 +17,6 @@ void main() {
String url;
Map surveyResult;

Map mockValidData() => {
'surveyId': faker.guid.guid(),
'question': faker.randomGenerator.string(50),
'answers': [{
'image': faker.internet.httpUrl(),
'answer': faker.randomGenerator.string(20),
'percent': faker.randomGenerator.integer(100),
'count': faker.randomGenerator.integer(1000),
'isCurrentAccountAnswer': faker.randomGenerator.boolean()
}, {
'answer': faker.randomGenerator.string(20),
'percent': faker.randomGenerator.integer(100),
'count': faker.randomGenerator.integer(1000),
'isCurrentAccountAnswer': faker.randomGenerator.boolean()
}],
'date': faker.date.dateTime().toIso8601String(),
};

PostExpectation mockRequest() => when(httpClient.request(
url: anyNamed('url'),
method: anyNamed('method')
Expand All @@ -49,7 +33,7 @@ void main() {
url = faker.internet.httpUrl();
httpClient = HttpClientSpy();
sut = RemoteLoadSurveyResult(url: url, httpClient: httpClient);
mockHttpData(mockValidData());
mockHttpData(FakeSurveyResultFactory.makeApiJson());
});

test('Should call HttpClient with correct values', () async {
Expand Down Expand Up @@ -81,7 +65,7 @@ void main() {
});

test('Should throw UnexpectedError if HttpClient returns 200 with invalid data', () async {
mockHttpData({'invalid_key': 'invalid_value'});
mockHttpData(FakeSurveyResultFactory.makeInvalidApiJson());

final future = sut.loadBySurvey();

Expand Down
Loading

0 comments on commit a3ce74c

Please sign in to comment.