对火箱的Unitest确保回电



我是Android&爪哇。我正在尝试编写一个单位测试,该测试验证了使用正确参数调用firebase方法&该特定的回电是在成功的

上开火的
public class RegistrationViewModel {
private iRegistrationViewCallback viewCallback;
private FirebaseAuth mAuth;
public  String email;
public String password;
public RegistrationViewModel(iRegistrationViewCallback view, FirebaseAuth mockAuth) {
    viewCallback = view;
    mAuth = mockAuth;
}
public void createUser() {
    mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    viewCallback.doSomething();
                }
            });
}
public void setEmail(String email) {
    this.email = email;
}
public void setPassword(String password) {
    this.password = password;
}
}

我的单位测试看起来像这样:

public class RegistrationViewModelUnitTest {
private RegistrationViewModel viewModelUnderTest;
private iRegistrationViewCallback mockView;
private FirebaseAuth mockAuth;

@Before
public void SetUp() {
    mockView = mock(iRegistrationViewCallback.class);
    mockAuth = mock(FirebaseAuth.getInstance().getClass());
    viewModelUnderTest = new RegistrationViewModel(mockView,mockAuth);
}
@Test
public void progressBarIsDismissedWhenRegistrationIsSuccessFul() throws Exception {
    String email = "somusername";
    String password = "somePassword";
    viewModelUnderTest.email = email;
    viewModelUnderTest.password = password;
    viewModelUnderTest.createUser();
    verify(mockAuth).createUserWithEmailAndPassword(email,password);
    verify(mockView).doSomething();
}
}

verify(mockView).doSomething();显然失败。

验证viewCallback.doSomething();成功的最佳方法是成功的。我应该以某种方式存根onComplete吗?还是有不同的方法?我想到了将firebase代码放置在接口后面,但仍然无法弄清楚如何验证呼叫是否被触发。我正在使用Mockito进行模拟。

这似乎更像是一个间的测试方案。

如果您要单元测试,我将进行以下操作:

步骤1)

使用回调行为创建一个公共类,而不是使用匿名类:

public class MyOnCompleteListener implements OnCompleteListener<AuthResult>() {
      @Override
      public void onComplete(@NonNull Task<AuthResult> task) {
          viewCallback.doSomething();
      }
});

步骤2)

将您的Createuser方法更改为:

public void createUser() {
    mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener((Executor) this
                       , new MyOnCompleteListener<AuthResult>() );
}

步骤3)

定义测试如下:

@Test
public void progressBarIsDismissedWhenRegistrationIsSuccessFul() throws Exception {
    String email = "somusername";
    String password = "somePassword";
    viewModelUnderTest.email = email;
    viewModelUnderTest.password = password;
    Task taskMock = mock(Task.class);
    when(mockAuth.createUserWithEmailAndPassword (email, password)).thenReturn(taskMock);
    viewModelUnderTest.createUser();
    verify(mockAuth, times(1)).createUserWithEmailAndPassword(email,password);
    verify(taskMock, times(1)).addOnCompleteListener(any(RegistrationViewModel.class)
          , any(MyOnCompleteListener.class));
}

最重要的是,您验证了createUserWithEmailAndPassword方法的某个回调。

如果您想将其作为单位测试,而无需任何实际调用框架代码。

相关内容

  • 没有找到相关文章

最新更新