2

我有一个在单击按钮时使用委托模式的片段。

class FutureMeetingEventViewFragment @Inject constructor(): Fragment() {

    @Inject
    lateinit var bundleUtilityModule: BundleUtilityModule
    lateinit var parcelableMeetingEvent: ParcelableMeetingEvent

    private var delegate: IEventDetailsDelegate? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)

        if(context is IEventDetailsDelegate) {
            delegate = context
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)

        val binding: ViewDataBinding = DataBindingUtil.inflate<ViewDataBinding>(inflater, R.layout.layout_future_meeting_event_fragment, container,false)
        DaggerUtilityModuleComponent.create().inject(this)
        this.parcelableMeetingEvent = this.bundleUtilityModule.getTypeFromBundle(BundleData.MEETING_EVENT_DATA.name, arguments)

        binding.setVariable(BR.meetingEvent, this.parcelableMeetingEvent)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        this.setCTAClickEvent()
    }

    private fun setCTAClickEvent() {
        future_event_card.setOnClickListener {
            delegate?.onEventClicked(this, this.parcelableMeetingEvent)
        }
    }
}

我在单元测试中可以看到的一个问题是,当我单击按钮时,因为 IEventDetailsDelegate 字段将为空,所以测试总是会失败。到目前为止,我的单元测试只是测试视图上是否显示了正确的数据:


@RunWith(AndroidJUnit4::class)
class GivenAFutureMeetingEventFragmentIsDisplayed  {

    private var fragmentId: Int = 0

    private lateinit var fragmentArgs: Bundle
    private lateinit var scenario: FragmentScenario<FutureMeetingEventViewFragment>

    @Before
    fun setup() {
        fragmentId = FutureMeetingEventViewFragment().id

        fragmentArgs = Bundle()
        fragmentArgs.apply {
            putParcelable(BundleData.MEETING_EVENT_DATA.name, ParcelableMeetingEvent(
                "Test Description",
                "Test Summary",
                "12344556",
                DateTime("2020-03-14T17:57:59+00:00"),
                DateTime("2020-03-14T18:57:59+00:00"),
                "Somewhere across the universe"
            ))
        }

        scenario = launchFragmentInContainer<FutureMeetingEventViewFragment>(
            fragmentArgs,
            fragmentId
        )
    }

    @Test
    fun thenACorrectlyMappedMeetingEventShouldBePassedToTheFragment() {
        onView(withId(R.id.spec_future_meeting_event_start_day)).check(matches(withText("Saturday")))
        onView(withId(R.id.spec_future_meeting_event_start_time)).check(matches(withText("5:57:59 PM")))
        onView(withId(R.id.spec_future_meeting_event_end_time)).check(matches(withText("6:57:59 PM")))
        onView(withId(R.id.spec_future_meeting_event_summary)).check(matches(withText("Test Summary")))
        onView(withId(R.id.spec_future_meeting_event_description)).check(matches(withText("Test Description")))
    }

    @Test
    fun thenTheContainerShouldBeClickable() {
        onView(withId(R.id.future_event_card)).check(matches(isClickable()))
    }
}

我想这篇文章有两个问题:

  1. 我可以模拟一个实现 IEventDetailsDelegate 的上下文并将其分配给我的模拟片段吗?
  2. 我应该在实现接口的实际活动上测试事件吗?
4

0 回答 0