File tree Expand file tree Collapse file tree 2 files changed +35
-9
lines changed
tests/Contexts/Mooc/Courses Expand file tree Collapse file tree 2 files changed +35
-9
lines changed Original file line number Diff line number Diff line change 1+ import { CourseRepository } from '../../../../../src/Contexts/Mooc/Courses/domain/CourseRepository' ;
2+ import { Course } from '../../../../../src/Contexts/Mooc/Courses/domain/Course' ;
3+ import { CourseId } from '../../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId' ;
4+ import { Nullable } from '../../../../../src/Contexts/Shared/domain/Nullable' ;
5+
6+ export class CourseRepositoryMock implements CourseRepository {
7+ private mockSave = jest . fn ( ) ;
8+ private mockSearch = jest . fn ( ) ;
9+
10+ async save ( course : Course ) : Promise < void > {
11+ this . mockSave ( course ) ;
12+ }
13+
14+ assertLastSavedCourseIs ( expected : Course ) : void {
15+ expect ( this . mockSave ) . toHaveBeenCalledWith ( expected ) ;
16+ }
17+
18+ async search ( id : CourseId ) : Promise < Nullable < Course > > {
19+ return this . mockSearch ( id ) ;
20+ }
21+
22+ whenSearchThenReturn ( value : Nullable < Course > ) : void {
23+ this . mockSearch . mockReturnValue ( value ) ;
24+ }
25+
26+ assertLastSearchedCourseIs ( expected : CourseId ) : void {
27+ expect ( this . mockSearch ) . toHaveBeenCalledWith ( expected ) ;
28+ }
29+ }
Original file line number Diff line number Diff line change 11import { CourseCreator } from '../../../../../src/Contexts/Mooc/Courses/application/CourseCreator' ;
2+ import { EventBus } from '../../../../../src/Contexts/Shared/domain/EventBus' ;
23import { CourseMother } from '../domain/CourseMother' ;
4+ import { CourseRepositoryMock } from '../__mocks__/CourseRepositoryMock' ;
35import { CreateCourseRequestMother } from './CreateCourseRequestMother' ;
4- import { CourseRepository } from '../../../../../src/Contexts/Mooc/Courses/domain/CourseRepository' ;
5- import { Course } from '../../../../../src/Contexts/Mooc/Courses/domain/Course' ;
6- import { EventBus } from '../../../../../src/Contexts/Shared/domain/EventBus' ;
76
8- let repository : CourseRepository ;
7+ let repository : CourseRepositoryMock ;
98let creator : CourseCreator ;
109
11- const createRepository = ( ) : CourseRepository => ( { save : jest . fn ( ) , search : jest . fn ( ) } ) ;
12- const eventBus = ( ) : EventBus => ( { publish : jest . fn ( ) } ) ;
13- const shouldSave = ( course : Course ) => expect ( repository . save ) . toHaveBeenCalledWith ( course ) ;
10+ const eventBus = ( ) : EventBus => ( { publish : jest . fn ( ) } ) ;
1411
1512beforeEach ( ( ) => {
16- repository = createRepository ( ) ;
13+ repository = new CourseRepositoryMock ( ) ;
1714 creator = new CourseCreator ( repository , eventBus ( ) ) ;
1815} ) ;
1916
@@ -24,5 +21,5 @@ it('should create a valid course', async () => {
2421
2522 await creator . run ( request ) ;
2623
27- shouldSave ( course ) ;
24+ repository . assertLastSavedCourseIs ( course ) ;
2825} ) ;
You can’t perform that action at this time.
0 commit comments