sniffer绑定适配器_使用数据绑定绑定适配器更容易测试

sniffer绑定适配器

The data binding library has been around for quite some time, however not that many developers use it. One of the main reasons behind this lack of adoption is the belief that it results in untestable code. However, when used correctly it can actually make testing your code easier, while also adding to increased code reusability. In this article we will go through some examples to see how we can use data binding effectively to achieve this and what to avoid doing.

吨他的数据绑定库已经有相当长的一段时间,但是没有那么多的开发人员使用它。 缺乏采用背后的主要原因之一是相信它会导致无法测试的代码。 但是,如果使用得当,它实际上可以使您的代码测试更加容易,同时还可以提高代码的可重用性。 在本文中,我们将通过一些示例来了解如何有效使用数据绑定来实现这一目标以及避免这样做。

将View逻辑与Presentation逻辑分开 (Separating View logic from Presentation logic)

A common use case that we encounter on most projects is handling the visibility of a view. Typically someone would write some code like the following to handle this.

我们在大多数项目中遇到的一个常见用例是处理视图的可见性。 通常,有人会编写如下代码来处理此问题。

class MainActivity : Activity() {
  ...
  private fun viewLogic() {
    if (data == null) {
      someView.visibility = View.GONE
    }
  }
  ...
}

This approach results in code that is difficult to test and requires Robolectric or Espresso. The logic of the if condition though, data == null, is presentation logic and can be moved to the view model where it can easily be tested. After doing so the code looks like this.

这种方法导致代码难以测试,并且需要Robolectric或Espresso。 但是if条件的逻辑data == null ,是表示逻辑,可以移到可以轻松测试的视图模型中。 这样做之后,代码如下所示。

class MainActivity : Activity() {
  ...
  private fun viewLogic() {
    if (!hasData) {
      someView.visibility = View.GONE
    }
  }
  ...
}
class MainViewModel(...) {
  ...
  private fun someFun(...) {
    _hasData.value = data != null 
  }
  ...
}

However testing the remaining view logic is still not easy, as code remains in our Activity. With data binding though, this piece of code can be made reusable and easy to test. But first let’s have a look at how someone might use data binding inappropriately by using the expression language provided by the library in xml.

但是,测试剩余的视图逻辑仍然不容易,因为代码仍保留在我们的Activity中。 但是,通过数据绑定,可以使这段代码可重用并且易于测试。 但是首先让我们看一下如何使用xml库提供的表达语言来不适当地使用数据绑定。

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    ...
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_logo"
            android:visibility="@{viewModel.hasData ? View.VISIBLE : View.GONE}" />
   ...
</layout>

This code can not be unit tested nor can it be reused. The expression language has some specific use cases, however this is not one of them and should not be used in most cases. Instead, we should utilise another feature of data binding called Binding Adapters. By making use of them, the code will now look like this.

此代码不能进行单元测试,也不能重复使用。 表达式语言有一些特定的用例,但这不是其中的一种,在大多数情况下都不应使用。 相反,我们应该利用数据绑定的另一个功能,即绑定适配器。 通过使用它们,代码现在将如下所示。

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    ...
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_logo"
            app:isVisible="@{viewModel.hasData}" />
   ...
</layout>
class BindingAdapters {


    @BindingAdapter("isVisible")
    fun isVisible(view: View, value: Boolean) {
        view.visibility = if (value) VISIBLE else GONE
    }
}

The view logic has now been moved to the BindingAdapters class and is now also reusable. It is also now much easier to test with Robolectric or a mocking library like MockK, as it is not tied to an Activity or Fragment lifecycle. The bindings in the xml can also be tested by creating some light weight, fast to run instrumentation tests.

现在,视图逻辑已移至BindingAdapters类,并且现在也可以重用。 现在,使用Robolectric或类似MockK的模拟库进行测试也变得更加容易,因为它不依赖于Activity或Fragment生命周期。 还可以通过创建一些轻量级,快速运行的仪器测试来测试xml中的绑定。

@RunWith(AndroidJUnit4::class)
class MainActivityBindingsTest {
    ...
    @Test @UiThreadTest
    fun testMainState() {
        val binding = ActivityMainBinding.inflate(targetContext)
        binding.viewModel = CountDownViewModelImpl(GetData(...))


        binding.executePendingBindings()


        assertEquals(binding.imageView.visibility, View.GONE)
  }
}

结论 (Conclusion)

As with every tool, in order for it to be effective it has to be used correctly. Following some simple rules when using data binding, your code can be easier to test and reuse, which also results in a code base that is easier to maintain.

与每种工具一样,为了使其有效,必须正确使用它。 使用数据绑定时遵循一些简单的规则,您的代码可以更容易测试和重用,这也导致代码库更易于维护。

翻译自: https://medium.com/kinandcartacreated/easier-testing-with-data-binding-bindingadapters-8f3b8bc8f90d

sniffer绑定适配器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值