@Before public void setup() { mockClock = mock(Clock.class); when(mockClock.millis()).thenReturn(0L); mockRepository = mock(WordRepository.class); when(mockRepository.getRandomWord()).thenReturn(Observable.just(testWord)); //noinspection unchecked mockStore = mock(Store.class); actionCreator = new ActionCreator(mockStore, mockRepository, mockClock); }
@Test public void testBottomLetterPressedWithCorrectAnswerAndErrorOccurs() { when(mockRepository.getRandomWord()).thenReturn(Observable.<Word>error(new Exception())); when(mockStore.state()) .thenReturn(createState("test", toListOfLetters("test"), singletonList("test"), 0, 1L)); actionCreator.bottomLetterPressed(); ArgumentCaptor<Action> actionCaptor = ArgumentCaptor.forClass(Action.class); verify(mockStore, times(3)).dispatch(actionCaptor.capture()); List<Action> capturedActions = actionCaptor.getAllValues(); assertEquals(capturedActions.get(0), Action.BottomPressed.INSTANCE); assertEquals(capturedActions.get(1), Action.Back.INSTANCE); assertEquals(capturedActions.get(2), Action.LoadWordError.INSTANCE); }
@Test public void testInitiateGameError() { when(mockRepository.getRandomWord()).thenReturn(Observable.<Word>error(new Exception())); actionCreator.initiateGame(); ArgumentCaptor<Action> actionCaptor = ArgumentCaptor.forClass(Action.class); verify(mockStore, times(3)).dispatch(actionCaptor.capture()); List<Action> capturedActions = actionCaptor.getAllValues(); Action.Navigate firstAction = (Action.Navigate) capturedActions.get(0); assertEquals(firstAction.getPage().getLayoutId(), R.layout.loading); assertEquals(firstAction.getAddToBackStack(), true); assertEquals(capturedActions.get(1), Action.Back.INSTANCE); assertEquals(capturedActions.get(2), Action.LoadWordError.INSTANCE); }