【问题标题】:How to test SingleLiveEvent<Void> variable?如何测试 SingleLiveEvent<Void> 变量?
【发布时间】:2019-03-06 10:50:09
【问题描述】:

我有一个SingleLiveEvent&lt;Void&gt; 变量。从 api 获得响应后,我将其发布。我的回调被调用并显示一个弹出窗口。 我的问题是我将如何编写一个测试用例进行检查,我的弹出窗口是否显示。

现场活动:

private SingleLiveEvent<Void> onAccountOverDrawn = new SingleLiveEvent<>();

我正在调用成功响应:

onAccountOverDrawn.post();

在我的片段中,我将其注册为

viewModel.getOnAccountOverDrawn().observe(this, aVoid -> onAccountOverDrawn());

onAccountOverDrawn() 我只是显示一个弹出窗口。

那么我将如何为这个场景编写一个测试用例呢?

当前测试用例:

@Test
public void updateApplicationStatus_AccountOverdrawn() {
    viewModel.updateApplicationStatus("AMOUNT_PENDING");

    assertNotNull(viewModel.getOnAccountOverDrawn()); //this line is of no use. Need to change this.
}

【问题讨论】:

  • 所以您想测试是否观察到 SingleLiveEvent?
  • 附带说明:不推荐使用SingleLiveEvent。请参阅此帖子以获得更好的选择:medium.com/androiddevelopers/…

标签: android unit-testing testing


【解决方案1】:

我这样解决这个问题:

  1. 获取 LiveData 并订阅我们的模拟观察者。
  2. 调用应该更改 ViewModel 中 LiveData 的方法。
  3. 检查我们的模拟观察者是否收到了更新的数据。
  4. 检查此模拟观察器是否没有更多更改。
  5. 检查如果我们在同一个 LiveData 上重新订阅这个模拟观察器,那么我们不会收到数据

见下面的代码:

@RunWith(MockitoJUnitRunner.class)
public class SomeFeatureViewModelTest {

    private SomeFeatureViewModel mSomeFeatureViewModel;

    // Rule for help testing. Just trust me you need it :)
    @Rule
    public InstantTaskExecutorRule mRule = new InstantTaskExecutorRule();

    @Mock
    private Observer<Void> mOnClickButtonEventObserver;

    @Before
    public void setup() {
        mSomeFeatureViewModel = new SomeFeatureViewModel();
    }

    @Test
    public void clickOnNextScreenButton() {

        // get LiveData and subscribe our observer to it:
        mSomeFeatureViewModel.getOnClickButtonEvent().observeForever(mOnClickButtonEventObserver);

        // call the method that should change the LiveData inside the ViewModel:
        mSomeFeatureViewModel.clickOnNextScreenButton();

        // check that our observer received the updated data:
        verify(mOnClickButtonEventObserver).onChanged(null);

        // check that there were no more changes of this observer:
        verifyNoMoreInteractions(mOnClickButtonEventObserver);

        // check that if we re-subscribe this observer on the same LiveData then we do not receive data:
        mSomeFeatureViewModel.getOnClickButtonEvent().observeForever(mOnClickButtonEventObserver);
        verifyNoMoreInteractions(mOnClickButtonEventObserver);

    }

}

【讨论】:

    【解决方案2】:

    如果您想测试是否观察到实时数据,您可以使用以下测试

    private LifecycleOwner lifecycleOwner;
    MutableLiveData<Boolean> mutableLiveData = new MutableLiveData(); //Live data to be observed from viewModel
    @Mock
    private Lifecycle lifecycle;
    
        @Before
        public void setup() {
            lifecycleOwner = getLifecycleOwner();
    PowerMockito.when(lifecycle.getCurrentState()).thenReturn(Lifecycle.State.CREATED);
        }
    
    
     @Test
        public void test() {
    
            mutableLiveData.observe(lifecycleOwner, new Observer<Boolean>() {
                @Override
                public void onChanged(@Nullable Boolean aBoolean) {
                    assertTrue(aBoolean);
                }
            });
            mutableLiveData.postValue(true);
        }
    
    
    private LifecycleOwner getLifecycleOwner() {
            return new LifecycleOwner() {
                @NonNull
                @Override
                public Lifecycle getLifecycle() {
                    return lifecycle;
                }
            };
        }
    

    【讨论】:

      【解决方案3】:

      如果要测试是否有弹窗,可以使用Robolectric。

      【讨论】:

        猜你喜欢
        • 2019-11-25
        • 1970-01-01
        • 1970-01-01
        • 2021-10-02
        • 2021-08-27
        • 1970-01-01
        • 2014-01-04
        • 2020-12-07
        • 1970-01-01
        相关资源
        最近更新 更多