我在Android应用中为我的Interactor类设置了适当的单元测试。这些课程是我应用程序的"业务逻辑"的地方。
这是一个这样的类:
public class ChangeUserPasswordInteractor {
private final FirebaseAuthRepositoryType firebaseAuthRepositoryType;
public ChangeUserPasswordInteractor(FirebaseAuthRepositoryType firebaseAuthRepositoryType) {
this.firebaseAuthRepositoryType = firebaseAuthRepositoryType;
}
public Completable changeUserPassword(String newPassword){
return firebaseAuthRepositoryType.getCurrentUser()
.flatMapCompletable(firebaseUser -> {
firebaseAuthRepositoryType.changeUserPassword(firebaseUser, newPassword);
return Completable.complete();
})
.observeOn(AndroidSchedulers.mainThread());
}
}
这是我写的测试:
@RunWith(JUnit4.class)
public class ChangeUserPasswordInteractorTest {
@Mock
FirebaseAuthRepositoryType firebaseAuthRepositoryType;
@Mock
FirebaseUser firebaseUser;
@InjectMocks
ChangeUserPasswordInteractor changeUserPasswordInteractor;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
RxAndroidPlugins.reset();
RxAndroidPlugins.setInitMainThreadSchedulerHandler(schedulerCallable -> Schedulers.trampoline());
}
@Test
public void changeUserPassword() {
Mockito.when(firebaseAuthRepositoryType.getCurrentUser()).thenReturn(Observable.just(firebaseUser));
Mockito.when(firebaseAuthRepositoryType.changeUserPassword(firebaseUser, "test123")).thenReturn(Completable.complete());
changeUserPasswordInteractor.changeUserPassword("test123")
.test()
.assertSubscribed()
.assertNoErrors()
.assertComplete();
}
}
我遇到的问题是,即使我将密码从test123"更改为" test123" intheruserpassword instokation,或者如果我在模拟返回" postalable.onerror(new thfforpable((中,则该测试都没有错误,即使我将密码更改为" test123",或者(。
我无法理解这种行为。有什么建议如何设置测试?
flatMapCompletable
的最后一行总是返回 Completable.complete()
应该是:
firebaseAuthRepositoryType.changeUserPassword(firebaseUser, newPassword);
so:
public Completable changeUserPassword(String newPassword){
return firebaseAuthRepositoryType.getCurrentUser()
.flatMapCompletable(firebaseUser ->
firebaseAuthRepositoryType.changeUserPassword(firebaseUser, newPassword));
}