开发者

Creating a mock API for third party library in statically typed language?

I have an application in VB/C# .NET which needs to interact with a third party API, but I would like to interface it 开发者_运维技巧out so I can use a mock API for testing purposes. Here are example API calls I would use in code:

RelayClient.AuthenticateUser(username, password, request, sessionID)
RelayClient.GetUserInfo(sessionID)

A few problems I am facing:

  1. RelayClient is NonInheritable/Static.
  2. RelayClient doesn't implement any interfaces.
  3. The API client library is closed source.

Is there any standard way of dealing with this situation?


The way to do this would be to make your code work against 'middle-man' non-static classes, which internally use the static classes from your 3rd party API. You then mock out these 'middle-man' classes for your unit tests.

Something like... (untested code)

// your consuming class
public class MyClass
{
    private readonly RelayClientManager manager;

    public MyClass(RelayClientManager manager)
    {
        this.manager = manager;
    }

    public UserInfo GetUserInfo(int sessionID)
    {
        return manager.GetUserInfo(sessionID);
    }    
}

// 'middle-man' class
public class RelayClientManager
{
    public UserInfo GetUserInfo(int sessionID)
    {
        return RelayClient.GetUserInfo(sessionID);
    }
}

// unit test (using xUnit.net and Moq)
[Fact]
public static void GetsUserInfo()
{
    // arrange
    var expected = new UserInfo();
    var manager = new Mock<RelayClientManager>();
    manager.Setup(x => x.GetUserInfo(0)).Returns(expected);
    var target = new MyClass(manager);        

    // act
    var actual = target.GetUserInfo(0);

    // assert
    Assert.Equal(expected, actual);
}


If you don't want to roll your own abstraction, have a look at TypeMock Isolator- it uses profiler hooks to do runtime class-level mocking of pretty much anything, whether you own it or not. It actually does a runtime substitution of the type implementation and intercepts all the calls (eg, you don't need to subclass the type- as far as the CLR is concerned, you ARE the type).

It's commercial, but they do have a free trial. Depending on how big the thing you need to mock is, the $799 can be well worth the time abstracting/wrapping the entire API surface if it's not interface-based.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜