开发者

How do I create an InvokeOperation for my Silverlight unit test?

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 i开发者_C百科sUnique. 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 InvokeOperation I recoded it thus:

public void CodeIsUnique(string code, Action<bool, Exception> action)
{
    return ObjectContext.CodeIsUnique(code, op =>
        {
            if (op.HasError)
            {
                action(false, op.Error);
                op.MarkErrorAsHandled();
            }
            else
            {
                action(op.Value, null);
            }
        }, null);
}

This meant that the view model code became:

this.ObjectInterface.CodeIsUnique(currentObject.Code, (result, error) =>
    {
        if (error != null)
        {
            // Deal with error
        }
        else if (!result)
        {
            // Code is not unique set error state.
        }
    }

and the test code could become:

public void CodeIsUnique(string code, Action<bool, Exception> action)
{
    bool isUnique = !_list.Any(o => string.Compare(o.Code, code) == 0);
    action(isUnique, null);
}

Therefore the code works as expected and I don't need to create an InvokeOperation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜