Skip to content

Instantly share code, notes, and snippets.

View phellipealexandre's full-sized avatar
🎯
Focusing

Phellipe Silva phellipealexandre

🎯
Focusing
View GitHub Profile
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
}
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
}
@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") }
}
@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)
@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()
@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") }
}
@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()
@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)
//----- Literal dummy -----
val dummyPrice = 10.0
//----- Generated dummy -----
val dummyCustomer = CustomerTestBuilder.build()
//----- Alternative empty implementation -----
val dummyNote = DummyNote()
class DummyNote(): Note //No implementation
@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)
}