chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I need to mock test a class library with a method that consumes a WCF operation. The operation is expected to return a list, which in turn is returned by the method. How do i make sure that the method works fine? Should i mock the wcf service and make it return a mock list? Please post some code sample to test this scenario.
asked by ragchen (600 points)

1 Answer

0 votes
You can use Isolator to have the method return a faked list of values (using the WillReturn), as well as faking the WCF connection. Please consider the following example:

[Test]
public void GetUsersTest()
{
    var fakeWcfClient = Isolate.Fake.Instance<WcfClient>();
    Isolate.WhenCalled(() => fakeWcfClient.GetUsers())
        .WillReturn(new List<User> { new User("TestUser1"), new User("TestUser2") });
    Isolate.Swap.NextInstance<WcfClient>().With(fakeWcfClient);

    UsersProvider provider = new UsersProvider();

    var users = provider.GetUsersFromWcf();

    Assert.AreEqual(2, users.Count);
}

public class UsersProvider
{
    public List<User> GetUsersFromWcf()
    {
        List<User> users;

        using (var client = new WcfClient())
        {
            users = client.GetUsers();
        }

        return users;
    }
}


Hope that helps.
answered by igal (5.7k points)
...