flow icon indicating copy to clipboard operation
flow copied to clipboard

Provide a sample unit test

Open msolomon opened this issue 11 years ago • 4 comments

I'm using mortar+flow and I want to test some transition logic in a Presenter, so my first instinct was to mock the Flow class and verify .goTo() is called appropriately. This won't easily work because I can't mock final classes such as Flow.

Do you have a recommendation on how to write such a test instead?

msolomon avatar Feb 14 '14 22:02 msolomon

  public static Flow createFlow(Object screen, Flow.Listener listener) {
    return new Flow(Backstack.single(screen), listener);
  }

  public static class MockFlowListener implements Flow.Listener {
    public Object lastScreen;
    public Flow.Direction lastDirection;

    @Override public void go(Backstack entries, Flow.Direction direction) {
      lastDirection = direction;
      lastScreen = entries.current().getScreen();
    }
  }

  private Presenter presenter;
  private MockFlowListener listener = new MockFlowListener();

  @Before public void setUp() {
    presenter = new Presenter(createFlow(new MyScreen(), listener));
  }

  @Test public void onTryAgainClicked() {
    presenter.onTryAgainClicked();
    assertThat(listener.lastScreen).isInstanceOf(SomeOtherScreen.class);
  }

ghost avatar Mar 27 '14 17:03 ghost

Actually, let's leave this open to remember to make a proper sample.

ghost avatar Mar 27 '14 17:03 ghost

Closing. Flow is not mockable, but Dispatcher can be mocked or stubbed. Making Flow non-final or an interface just isn't practical while the API is in such flux.

loganj avatar Apr 13 '15 16:04 loganj

Thus isn't about making flow non-final, it's about demonstrating how to unit test it.

rjrjr avatar Apr 15 '15 13:04 rjrjr