New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mocked object not being used #66
Comments
Hi, unfortunately if you use Dagger Android you can replace only objects defined in the application component. |
Ok thanks. |
It depends on the kind of test you want to write and how you organize the code. Usually I try to keep the presenter/viewModel as simple as possible and move the logic to other objects (that can be defined in the application component). Then in the espresso test I use the real presenter/viewModel mocking the other objects. You don't need to use a single AppModule, you can split it in multiple modules. |
I having the same issue here. So does that mean daggermock doesnt work for injecting mocked presenters for activities? i have already created a separate test module and test app component that has all the provides metthods inside one appModule in the base of the test app component yet it cant find and use that? |
If you are using Dagger Android you can't use DaggerMock to replace the presenter/viewModel in an espresso test. However it's not easy to replace it without DaggerMock eather. |
I have tried implementing everything exactly as the sample project - not sure what I am missing, or maybe I am just not understanding how Mockito works...
I am injecting mock presenter into my test, but when test runs the activity is not using the mocked presenter.
If I debug the test and I inspect the presenter, and the activity's presenter I can see that they are not the same:
Debug Watches:
presenter (in test) = SealCheckContract$Presenter$MockitoMock$999498069
mActivityTestRule.getActivity().presenter = SealCheckPresenter
The activity calls a method on the presenter,
presenter.isEarphonesConnected()
, I need this to always return true.So in my test I have:
when(presenter.isEarphonesConnected()).thenReturn(true);
If I do
presenter.isEarphonesConnected()
in the test it returnstrue
correctly, but in the activity it still returnsfalse
(obviously because it is not using the mocked presenter)I am using Dagger Android (so I used the
EspressoDaggerMockRule
as per the documentation)Any ideas on what I could be doing wrong? How do I force the activity to use the mocked presenter?
Edit: Is it because of this?
objects defined in subcomponent/dependent component cannot be replaces, DaggerMock works only for objects defined in application component
If so, how would I then replace the presenter?
The text was updated successfully, but these errors were encountered: