Unit testing DevForce EntityManager.InvokeServerMethod

64 Views Asked by At

In a WPF application I have a view model that retrieves data from the server using a DevForce Entity Manager InvokeServerMethod call. I would like to unit test by view model.

Using Moq, I mocked the IEntityManagerProvider and setup the InvokeServerMethod.

var mockManager = new Mock<MyEntities>(MockBehavior.Loose, false, null, 
    EntityServiceOption.UseDefaultService, "Fake");

var mockProvider = new Mock<IEntityManagerProvider<MyEntities>>(MockBehaviour.Loose);

mockProvider.Setup(m => m.Manager).Returns(mockManager.Object);

mockManager
    .Setup(m => m.InvokeServerMethod(It.IsAny<ServerMethodDelegate>())
    .Returns(new List<int>());

But when I run the test, I get

"Invalid setup on non-virtual member".

Next, I went into my IEntityManagerProvider interface and created this method

object MockableServerMethod(ServerMethodDelegate method, params object[] 
    parameters); 

Then I implemented this new method

public object MockableServerMethod(ServerMethodDelegate method, params 
    object[] parameters)
{
    return Manager.InvokeServerMethod(method, params);
}

This allows me to mock the server call, but requires me to change the production code, which I really shouldn't have to do.

What is the correct way to write a unit test when the method being tested calls InvokeServerMethod()?

1

There are 1 best solutions below

0
Dave Shine On

I came up with a cleaner solution, although it still requires a change to production code.

MyEntities is a partial class that inherits from IdeaBlade.EntityModel.EntityManager. Therefore, I was able to add the following code to MyEntities

public virtual async Task<object>ExecuteServerMethodAsync(
    ServerMethodDelegate method, params object[] parameters)
{
    return await InvokeServerMethodAsync(method, parameters);
}

Now, my production code calls Execute... rather than Invoke... and the Execute method is mockable.

Dave