单元测试碎片导致代码膨胀

时间:2017-04-11 15:42:49

标签: android unit-testing android-fragments

Fragment上实施单元测试后,我的课程代码长度增加了一倍。我应该遵循某些最佳实践,还是我过度测试?这应该是一个实施测试吗?

特别值得注意的是方法connectToBehaviorRelay(behaviorRelay, artistResultFunction, getConsumer(), mySchedulerProvider);。我可以删除其中的一些参数,但我必须创建更多的@VisibleForTesting方法。

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        ArtistReleasesActivity.component.inject(this);
        View view = inflater.inflate(R.layout.fragment_recyclerview, container, false);
        ButterKnife.bind(this, view);
        connectToBehaviorRelay(behaviorRelay, artistResultFunction, getConsumer(), mySchedulerProvider);
        setupRecyclerView(getActivity());
        return view;
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    public void connectToBehaviorRelay(BehaviorRelay<List<ArtistRelease>> behaviorRelay, ArtistResultFunction artistResultFunction, Consumer<List<ArtistRelease>> consumer, SchedulerProvider mySchedulerProvider)
    {
        behaviorRelay
                .map(artistResultFunction.map(getArguments().getString("map")))
                .observeOn(mySchedulerProvider.ui())
                .subscribe(consumer);
    }

    private Consumer<List<ArtistRelease>> getConsumer()
    {
        return o ->
        {
            rvReleasesAdapter.setRemixes(o);
            rvReleasesAdapter.notifyDataSetChanged();
        };
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    public void setupRecyclerView(FragmentActivity activity)
    {
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        rvReleasesAdapter = new RecyclerViewReleasesAdapter(activity);
        recyclerView.setAdapter(rvReleasesAdapter);
    }

    @VisibleForTesting
    public void setArtistResultFunction(ArtistResultFunction artistResultFunction)
    {
        this.artistResultFunction = artistResultFunction;
    }

    @VisibleForTesting
    public void setRecyclerView(RecyclerView recyclerView)
    {
        this.recyclerView = recyclerView;
    }

我的问题是:我是否需要拥有所有这些内容,还是应该将某些内容移至Presenter?将它们置于Fragment onCreateView()范围内似乎会使事情复杂化。

0 个答案:

没有答案