使用计算机拥有的状态进行单元测试 [英] Unit Testing With Computer Owned States

查看:23
本文介绍了使用计算机拥有的状态进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的计算机何时接听/拨打电话编写单元测试.

I am writing a unit test for when my computer receives/makes a phone call.

正在测试的方法是处理传入/传出呼叫的事件.如果来电者不是被批准的来电者,则该来电将被拒绝.

The methods being tested are the events that handle the incoming/outgoing call. If the caller is not an approved caller then the call is rejected.

代码工作正常,但我真的找不到任何可以测试我的单元测试的东西.问题是我的电脑是否在通话中"的实际状态由我的班级控制.只有计算机知道当前是否接通了电话.

The code works fine, but I can't really find anything to test against for my unit test. The problem is that the actual state of "if my computer is in a call or not" is not controlled my by class. Only the computer knows if a call is currently connected or not.

我希望有一些单元测试大师可以告诉我如何测试这个场景.我不想创建一个与我的代码无关的虚拟变量,只是为了让我的单元测试通过.

I am hoping that there are some Unit Test Guru's out there than can tell me what to do to test this scenario. I do not want to create a dummy var that has no relation to my code just to make my unit test pass.

为了更具体一点,这是我的单元测试:

To make this a bit more concrete here is my unit test:

    private DeviceMediator deviceMediator;
    private IDeviceControlForm deviceControlForm;
    private IDataAccess data;
    private ICallMonitor callMonitor; 

    // Use TestInitialize to run code before running each test 
    [TestInitialize()]
    public void MyTestInitialize()
    {
        deviceControlForm = MockRepository.GenerateStub<IDeviceControlForm>();          
        data = MockRepository.GenerateStub<IDataAccess>();
        callMonitor = MockRepository.GenerateStub<ICallMonitor>();

        deviceMediator = new DeviceMediator(deviceControlForm, data) 
           {CallMonitor = callMonitor};
    }

    [TestMethod]
    public void TestHandleIncomingCall()
    {
        //Arrange

        //Act
        deviceMediator.OnIncomingCall(null, new CallState(), 
           new CallInfoState());

        //Assert
        // I could not find anything to feasably test on this.  
        Assert.IsTrue(true);
    }

这是它调用的方法:

public void OnIncomingCall(Call call, CallState callState, 
    CallInfoState callInfoState)
{
    // See if this call is on our list of approved callers
    bool callApproved = false;
    foreach (PhoneContact phoneContact in Whitelist)
    {
        if (phoneContact.PhoneNumber == call.CallerID)
            callApproved = true;
    }

    // If this is not an approved call then 
    if (!callApproved)
        CallMonitor.Hangup();
}

推荐答案

原来我只是对 Rhino Mocks 了解得不够多.这个问题的答案可以在这里.

Turns out I just did not know enough about Rhino Mocks. The answer to this issue can be found here.

这是包含该答案的我的代码.

This is my code with that answer incorporated.

单元测试:

    [TestMethod]
    public void TestHandleIncomingCall()
    {
        //Arrange
        callMonitor.InCall = true;
        // This is the magic.  When Hangup is called I am able to set
        // the stub's InCall value to false.
        callMonitor.Expect(x => x.Hangup()).Callback(() => WhenCalled(invocation =>
                                                       {
                                                           callMonitor.InCall = false;
                                                       });
        List<PhoneContact> whiteList = FillTestObjects.GetSingleEntryWhiteList();
        data.Expect(x => x.GetWhiteListData()).Return(whiteList);
        const string invalidPhoneNumber = "123";

        //Act
        deviceMediator.HandleIncomingCall(invalidPhoneNumber);

        //Assert
        Assert.IsFalse(callMonitor.InCall);
    }

我不得不把我的代码改成这样,因为 Call 有一个内部构造函数:

I had to change my code to this because Call has an internal constructor:

    public void OnIncomingCall(Call call, CallState callState, 
       CallInfoState callInfoState)
    {
        // See if this call is on our list of approved callers
        HandleIncomingCall(call.CallerID);
    }

    public void HandleIncomingCall(string callNumber)
    {
        bool callApproved = false;
        foreach (PhoneContact phoneContact in Whitelist)
        {
            if (phoneContact.PhoneNumber == callNumber)
                callApproved = true;
        }

        // If this is not an approved call then 
        if (!callApproved)
            CallMonitor.Hangup();
    }

这篇关于使用计算机拥有的状态进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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