对广播接收器进行单元测试? [英] Unit testing a broadcast receiver?

查看:61
本文介绍了对广播接收器进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的项目中的BroadcastReceiver,我希望对其进行单元测试.当用户拨打电话时,它会获取电话号码,并设置一个意图来开始新活动,并传入电话号码.

Here's a BroadcastReceiver from my project, which I'm looking to unit test. When the user makes a phone call, it grabs the phone number, and sets up an intent to start a new activity, passing in the phone number.

public class OutgoingCallReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context xiContext, Intent xiIntent) 
    {
        if (xiIntent.getAction().equalsIgnoreCase(Intent.ACTION_NEW_OUTGOING_CALL))
        {
            String phoneNum = xiIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

            Intent intent = new Intent(xiContext, MyActivity.class);
            intent.putExtra("phoneNum", phoneNum);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            xiContext.startActivity(intent);
            setResultData(null);
        }
    }
}

到目前为止,我的单元测试如下:

So far, my unit test looks like this:

public class OutgoingCallReceiverTest extends AndroidTestCase
{
    private OutgoingCallReceiver mReceiver;

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();

        mReceiver = new OutgoingCallReceiver();
    }

    public void testStartActivity()
    {
        Intent intent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
        intent.putExtra(Intent.EXTRA_PHONE_NUMBER, "01234567890");

        mReceiver.onReceive(getContext(), intent);        
    }
}

这贯穿了代码,但是我希望我的测试能够检查意图是否已发出,并检查其上的电话号码.我该怎么做?

This runs through the code, but I want my test to be able to check that the intent was sent out, and to check the phone number on it. How do I do this?

我还可以测试电话是否被取消(由于setResultData(null)行)?

Can I also test that the phone call gets cancelled (because of the setResultData(null) line)?

推荐答案

马特,

听起来像您需要模拟一个Context ...,然后将您的方法切换到接受接口而不是具体类:public void onReceive(IContext c, IIntent i),仅用于测试目的.但是Context和Intent类不是您的类,而是Android的类,因此您不能仅仅"使它们实现您的接口,因此必须包装"它们才能公开一个您的界面,这是很多代码而没有太多收获.非常讨厌!

Sounds like you need to mock-up a Context ... and then swap your methods over to accepting interfaces instead of concrete classes: public void onReceive(IContext c, IIntent i), just for the purposes of testing. But then the Context and Intent classes aren't yours are they... they're Android's... so you can't "just" make them implement your interfaces, so you'd have to "wrap" them in order to expose a your interface, which is RATHER a lot of code for not much gain. Very Yucky!!!

因此,我开始怀疑是否有人曾经经历过所有这些事情,并为我们做了艰苦的努力...和tada:http://developer.android.com/reference/android/test/mock/package-summary.html

So I started to wonder if someone's been through all this before, and done the hard-yards for us... and tada: http://developer.android.com/reference/android/test/mock/package-summary.html

干杯.基思.

这篇关于对广播接收器进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆