views:

87

answers:

3

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);
    }

and here is the method it is calling:

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();
}
A: 

Here's a couple things you could do:

  1. You could create a mock WhiteList collection that will contain a certain set of the PhoneContacts, and then call OnIncomingCall with PhoneContacts that are or are not in the WhiteList and then check the CallMonitor (or the Call object itself, I suppose) to see if the call is in the correct state. So Assert.IsTrue(call.IsConnected) or Assert.IsFalse(call.IsConnected) depending on the scenario you're testing.
  2. You could also see if the method handles Call objects that are null or that are not in the correct state at the point where this method is being called.
Mike Hall
+1  A: 

It's called dependancy injection. You make your code act on an interface that mimics the real Phone api, and supply your own controllable implementation for the tests. There are systems out there like Spring.Net that make this easier to do.

Joel Coehoorn
+2  A: 

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.

Unit Test:

    [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);
    }

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();
    }
Vaccano