I’m trying to mock up DomainContext calls for my unit test.
My real code is:
public class SomeDomainService : LinqToSqlDomainService<SomeDataContext>
{
[Invoke]
public bool CodeIsUnique(string code)
{
return !this.DataContext.Objects.Any(o => string.Compare(o.Code, code, true) == 0);
}
}
This is called on the client side through this code which is implements an interface:
public InvokeOperation<bool> CodeIsUnique(string code, Action<InvokeOperation<bool>> action, object userState)
{
return ObjectContext.CodeIsUnique(code, action, userState);
}
Where ObjectContext is an instance of an auto-generated class derived from System.ServiceModel.DomainServices.Client.DomainContext. When this is called from the view model the action code is executed:
this.ObjectInterface.CodeIsUnique(currentObject.Code, op =>
{
if (!op.Value)
{
// Code is not unique set error state.
}
}
So I’ve created a mock implementation which doesn’t go to the server:
public InvokeOperation<bool> CodeIsUnique(string code, Action<InvokeOperation<bool>> action, object userState)
{
bool isUnique = !_list.Any(o => string.Compare(o.Code, code) == 0);
InvokeOperation<bool> op = ?????; // Here's the problem
action.Invoke(op);
return op;
}
This works off a local List<Object> and calling action.Invoke works, but with just null in the call the view model code fails as op is null (obviously).
So what I need to do is create an object of type InvokeOperation<bool> and set the .Value property to isUnique. However, I can’t work out how to to this. In the first instance the .Value property is read-only and in the second, there’s no public creator for InvokeOperation<T>.
I don’t really want to create a test DomainService in my test web project if I can help it.
OK, I solved this in a different way. Rather than trying to pass around
InvokeOperationI recoded it thus:This meant that the view model code became:
and the test code could become:
Therefore the code works as expected and I don’t need to create an
InvokeOperation.