Unit-testing WCF: Client Side
Original Method

The following sample shows a method that create a new service using WCF objects, IServiceListProvider,
which we call and return a string containing a list of our favorite sources.

C#

public string GetSourceListFromServer(string source)
{
    // Get the channel from WCF
    EndpointAddress address = new EndpointAddress("http://typemock.com/service.svc");
    WSHttpBinding binding = new WSHttpBinding();

    ChannelFactory<ISourceListProvider> factory =
        new ChannelFactory<ISourceListProvider> (binding, address);

    ISourceListProvider channel = factory.CreateChannel();

    // Call the service
    string result = channel.GetSourceList(source);

    // Analyze the result
    if (result == string.Empty)
        return "Error";

    return result;
}

VB
Public Function GetSourceListFromServer(source As String) As String
   ' Get the channel from WCF
   Dim address As New EndpointAddress("http://typemock.com/service.svc")
   Dim binding As New WSHttpBinding()

   Dim factory As New ChannelFactory(Of ISourceListProvider)(binding, address)

   Dim channel As ISourceListProvider = factory.CreateChannel()

   ' Call the service
   Dim result As String = channel.GetSourceList(source)

   ' Analyze the result
   If result = String.Empty Then
       Return "Error"
   End If

   Return result
End Function

Typemock Isolator Features Used

Swap Future Instance

Changing methods behavior

Scenario

1. Get a handle for future ChannelFactory<ISourceListProvider> class.

2. Set all calls to the method ChannelFactory<ISourceListProvider>.CreateChannel().GetSourceList(string source) to be ignored and to return an empty string.

3. Create an instance of the calss under test, DataProviderClient and call the GetSourceListFromServer method.

4. Check if the result is as anticipated.

Code
C#
[TestMethod, Isolated]
public void GetSourceListFromServer_SimulateEmptyReturn_ReturnError()
{
    // Arrange
    // Fake the Factory object that's created in the method and modify his methods behavior
    var fakeFactory = Isolate.Fake.NextInstance<ChannelFactory<ISourceListProvider>>();
    Isolate.WhenCalled(() => fakeFactory.CreateChannel().GetSourceList("AnyValue")).WillReturn(string.Empty);

    // Act
    DataProviderClient client = new DataProviderClient();
    string result = client.GetSourceListFromServer("AnyServer");

    // Assert
    Assert.AreEqual("Error", result);
}

VB
<TestMethod(), Isolated()>
Public Sub GetSourceListFromServer_SimulateEmptyReturn_ReturnError()
    ' Arrange
    ' Fake the Factory object that's created in the method and modify his methods behavior
    Dim fakeFactory = Isolate.Fake.NextInstance(Of ChannelFactory(Of ISourceListProvider))()
    Isolate.WhenCalled(Function() fakeFactory.CreateChannel().GetSourceList("AnyValue")).WillReturn(String.Empty)

    ' Act
    Dim client As New DataProviderClient()
    Dim result As String = client.GetSourceListFromServer("AnyServer")

    ' Assert
    Assert.AreEqual("Error", result)
End Sub