This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface NoteApi { | |
suspend fun uploadNote(note: Note): Result | |
suspend fun fetchAllNotes(): List<Note> | |
} | |
class RealNoteApi : NoteApi { | |
override suspend fun uploadNote(note: Note): Result { | |
//Real implementation | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface NoteApi { | |
suspend fun uploadNote(note: Note): Result | |
suspend fun fetchAllNotes(): List<Note> | |
} | |
class RealNoteApi: NoteApi { | |
override suspend fun uploadNote(note: Note): Result { | |
//Real impl | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `Track analytics event when creating new note`() { | |
val analyticsWrapperMock = mockk<AnalyticsWrapper>() //Mock Double | |
val noteAnalytics = NoteAnalytics(analyticsWrapperMock) | |
noteAnalytics.trackNewNoteEvent(NoteType.Supermarket) | |
//Observes that specific operation has happened | |
verify(exactly = 1) { analyticsWrapperMock.logEvent("NewNote", "SuperMarket") } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `Retrieve notes count from server when requested`() { | |
val notesApiStub = mockk<NotesApi>() //Stub Double | |
val noteRepository = NoteRepository(notesApiStub) | |
val note = generateDummyNote() //Dummy Double | |
every { notesApiStub.fetchAllNotes() } returns listOf(note, note) | |
val allNotesCount = noteRepository.getNoteCount() | |
assertEquals(expected = 2, actual = allNotesCount) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `Track analytics event when creating new note`() { | |
val analyticsWrapperSpy = //Spy | |
val noteAnalytics = NoteAnalytics(analyticsWrapperSpy) //System under test | |
//AnalyticsWrapperSpy records the interaction with NoteAnalytics under the hoods | |
noteAnalytics.trackCreateNewNoteEvent(NoteType.Supermarket) | |
//Based on the its internal implementation, the spy returns the state of the dependency | |
val numberOfEvents = analyticsWrapperSpy.getNewNoteEventsRegistered() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `Track analytics event when creating new note`() { | |
val analyticsWrapperMock = //Mock | |
val noteAnalytics = NoteAnalytics(analyticsWrapperMock) //System under test | |
noteAnalytics.trackNewNoteEvent(NoteType.Supermarket) | |
//Verifies that specific call has happened | |
verify(exactly = 1) { analyticsWrapperMock.logEvent("NewNote", "SuperMarket") } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `Retrieve all notes when requested`() { | |
val noteApiFake = FakeNoteApi() //Fake double implementing the same interface as the original | |
val noteRepository = NoteRepository(noteApiFake) //System under test | |
val note = //Dummy | |
noteApiFake.uploadNote(note) //Configuring the fake | |
noteApiFake.uploadNote(note) //Configuring the fake | |
//Fake with real and lightweight implementation is going to be used under the hoods | |
val allNotes = noteRepository.getNotes() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `Retrieve notes count from server when requested`() { | |
val notesApiStub = //Stub | |
val note = //Dummy | |
val noteRepository = NoteRepository(notesApiStub) //System under test | |
//Stub configuration. Hard-coded value returned will be a list with 2 entries. | |
//This method is going to be called by noteRepository.getNoteCount() | |
every { notesApiStub.fetchAllNotes() } returns listOf(note, note) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----- Literal dummy ----- | |
val dummyPrice = 10.0 | |
//----- Generated dummy ----- | |
val dummyCustomer = CustomerTestBuilder.build() | |
//----- Alternative empty implementation ----- | |
val dummyNote = DummyNote() | |
class DummyNote(): Note //No implementation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `Update registered note count when registering a new note in empty repository`() { | |
val dummyNote = //Dummy | |
noteRepository.registerNote(dummyNote) //Just filling the parameter, the double's content is not relevant for the test | |
val allNotes = noteRepository.getNotes() | |
assertEquals(expected = 1, actual = allNotes.size) | |
} |
NewerOlder