Skip to content

Commit daab348

Browse files
committed
Use ObjectMother pattern in tests
1 parent 4bacbfa commit daab348

File tree

10 files changed

+130
-15
lines changed

10 files changed

+130
-15
lines changed
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import { FileCourseRepository } from '../../../../../src/Contexts/Mooc/Courses/infrastructure/FileCourseRepository';
2-
import { Course } from '../../../../../src/Contexts/Mooc/Courses/domain/Course';
3-
import { CourseId } from '../../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId';
4-
import { CourseName } from '../../../../../src/Contexts/Mooc/Courses/domain/CourseName';
5-
import { CourseDuration } from '../../../../../src/Contexts/Mooc/Courses/domain/CourseDuration';
2+
import { CourseMother } from '../../../../Mooc/Courses/domain/CourseMother';
63

74
describe('Save Course', () => {
85
it('should have a course', async () => {
96
const repository = new FileCourseRepository();
10-
const course = new Course(
11-
new CourseId('0766c602-d4d4-48b6-9d50-d3253123275e'),
12-
new CourseName('name'),
13-
new CourseDuration('duration')
14-
);
7+
const course = CourseMother.random();
158

169
await repository.save(course);
1710
});
@@ -20,11 +13,7 @@ describe('Save Course', () => {
2013
describe('Search Course', () => {
2114
it('should return an existing course', async () => {
2215
const repository = new FileCourseRepository();
23-
const course = new Course(
24-
new CourseId('0766c602-d4d4-48b6-9d50-d3253123275e'),
25-
new CourseName('name'),
26-
new CourseDuration('duration')
27-
);
16+
const course = CourseMother.random();
2817

2918
await repository.save(course);
3019

@@ -34,6 +23,6 @@ describe('Search Course', () => {
3423
it('should not return a non existing course', async () => {
3524
const repository = new FileCourseRepository();
3625

37-
expect(await repository.search(new CourseId('de8c20b5-1181-415b-bb82-1f15bf1b67f1'))).toBeFalsy();
26+
expect(await repository.search(CourseMother.random().id)).toBeFalsy();
3827
});
3928
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { CourseCreator } from '../../../../src/Contexts/Mooc/Courses/application/CourseCreator';
2+
import { CourseMother } from '../domain/CourseMother';
3+
import { CreateCourseRequestMother } from './CreateCourseRequestMother';
4+
import { CourseRepository } from '../../../../src/Contexts/Mooc/Courses/domain/CourseRepository';
5+
import { Course } from '../../../../src/Contexts/Mooc/Courses/domain/Course';
6+
7+
let repository: CourseRepository;
8+
let creator: CourseCreator;
9+
10+
const createRepository = (): CourseRepository => ({ save: jest.fn(), search: jest.fn() });
11+
const shouldSave = (course: Course) => expect(repository.save).toHaveBeenCalledWith(course);
12+
13+
beforeEach(() => {
14+
repository = createRepository();
15+
creator = new CourseCreator(repository);
16+
});
17+
18+
it('should create a valid course', async () => {
19+
const request = CreateCourseRequestMother.random();
20+
21+
const course = CourseMother.fromRequest(request);
22+
23+
await creator.run(request);
24+
25+
shouldSave(course);
26+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { CreateCourseRequest } from '../../../../src/Contexts/Mooc/Courses/application/CreateCourseRequest';
2+
import { CourseDuration } from '../../../../src/Contexts/Mooc/Courses/domain/CourseDuration';
3+
import { CourseId } from '../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId';
4+
import { CourseName } from '../../../../src/Contexts/Mooc/Courses/domain/CourseName';
5+
import { CourseDurationMother } from '../domain/CourseDurationMother';
6+
import { CourseIdMother } from '../../Shared/domain/Courses/CourseIdMother';
7+
import { CourseNameMother } from '../domain/CourseNameMother';
8+
9+
export class CreateCourseRequestMother {
10+
static create(id: CourseId, name: CourseName, duration: CourseDuration): CreateCourseRequest {
11+
return { id: id.value, name: name.value, duration: duration.value };
12+
}
13+
14+
static random(): CreateCourseRequest {
15+
return this.create(CourseIdMother.random(), CourseNameMother.random(), CourseDurationMother.random());
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { CourseDuration } from '../../../../src/Contexts/Mooc/Courses/domain/CourseDuration';
2+
import { WordMother } from '../../../Shared/domain/WordMother';
3+
4+
export class CourseDurationMother {
5+
static create(value: string): CourseDuration {
6+
return new CourseDuration(value);
7+
}
8+
9+
static random(): CourseDuration {
10+
return this.create(WordMother.random());
11+
}
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { CourseId } from '../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId';
2+
import { CourseName } from '../../../../src/Contexts/Mooc/Courses/domain/CourseName';
3+
import { CourseDuration } from '../../../../src/Contexts/Mooc/Courses/domain/CourseDuration';
4+
import { Course } from '../../../../src/Contexts/Mooc/Courses/domain/Course';
5+
import { CreateCourseRequest } from '../../../../src/Contexts/Mooc/Courses/application/CreateCourseRequest';
6+
import { CourseIdMother } from '../../Shared/domain/Courses/CourseIdMother';
7+
import { CourseNameMother } from './CourseNameMother';
8+
import { CourseDurationMother } from './CourseDurationMother';
9+
10+
export class CourseMother {
11+
static create(id: CourseId, name: CourseName, duration: CourseDuration): Course {
12+
return new Course(id, name, duration);
13+
}
14+
15+
static fromRequest(request: CreateCourseRequest): Course {
16+
return this.create(
17+
CourseIdMother.create(request.id),
18+
CourseNameMother.create(request.name),
19+
CourseDurationMother.create(request.duration)
20+
);
21+
}
22+
23+
static random(): Course {
24+
return this.create(CourseIdMother.random(), CourseNameMother.random(), CourseDurationMother.random());
25+
}
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { CourseName } from '../../../../src/Contexts/Mooc/Courses/domain/CourseName';
2+
import { WordMother } from '../../../Shared/domain/WordMother';
3+
4+
export class CourseNameMother {
5+
static create(value: string): CourseName {
6+
return new CourseName(value);
7+
}
8+
9+
static random(): CourseName {
10+
return this.create(WordMother.random());
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { CourseId } from '../../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId';
2+
import { UuidMother } from '../../../../Shared/domain/UuidMother';
3+
4+
export class CourseIdMother {
5+
static create(value: string): CourseId {
6+
return new CourseId(value);
7+
}
8+
9+
static random(): CourseId {
10+
return this.create(UuidMother.random());
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as faker from 'faker';
2+
3+
export class MotherCreator {
4+
static random(): Faker.FakerStatic {
5+
return faker;
6+
}
7+
}

tests/Shared/domain/UuidMother.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { MotherCreator } from './MotherCreator';
2+
3+
export class UuidMother {
4+
static random(): string {
5+
return MotherCreator.random().random.uuid();
6+
}
7+
}

tests/Shared/domain/WordMother.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { MotherCreator } from './MotherCreator';
2+
3+
export class WordMother {
4+
static random(): string {
5+
return MotherCreator.random().lorem.word();
6+
}
7+
}

0 commit comments

Comments
 (0)