6

我有一个单元测试来验证当使用某些 eventArgs 触发事件时对象(例如Foo)将调用某些方法(例如)。Bar为了模拟上述方法,我使用虚拟和存根Foo

Mock<Foo> stubbedFoo = new Mock<Foo>(mockEventProvider);

mockEventProvider.Raise( x => x.MyEvent += null, myEventArgs ) //fire the event
stubbedFoo.Verify(foo => foo.Bar()); verify Bar is called as a result

但是,上面失败了,Bar 不会被调用,显然是因为 Foo 对象不是事件构造的。但是,如果我添加如下一行:

Mock<Foo> stubbedFoo = new Mock<Foo>(mockEventProvider);
var workAround = stubbedFoo.Object //adding this workaround will work
mockEventProvider.Raise( x => x.MyEvent += null, myEventArgs ) //fire the event
stubbedFoo.Verify(foo => foo.Bar()); verify Bar is called as a result

它会起作用,因为在 .Object 上调用 get 显然会强制 mock 构造对象。有没有比添加这条线更优雅的解决方案?

4

1 回答 1

1

我不认为你可以。我检查了最小起订量源并戳穿它,它看起来不像城堡的代理拦截器实际上是在你调用之前创建的.Object。看看这个踪迹:

public object Object
{
    get { return this.GetObject(); }
}

private object GetObject()
{
    var value = this.OnGetObject();
    this.isInitialized = true;
    return value;
}

其次是

protected override object OnGetObject()
{
    if (this.instance == null)
    {
        this.InitializeInstance();
    }

    return this.instance;
}

这样做:

private void InitializeInstance()
{
    PexProtector.Invoke(() =>
    {
        this.instance = proxyFactory.CreateProxy<T>(
            this.Interceptor,
            this.ImplementedInterfaces.ToArray(),
            this.constructorArguments);
    });
}

ProxyFactory 实际创建对象并将其包装在代理中

public T CreateProxy<T>(ICallInterceptor interceptor, Type[] interfaces, object[] arguments)
{
    var mockType = typeof(T);

    if (mockType.IsInterface)
    {
        return (T)generator.CreateInterfaceProxyWithoutTarget(mockType, interfaces, new Interceptor(interceptor));
    }

    try
    {
        return (T)generator.CreateClassProxy(mockType, interfaces, new ProxyGenerationOptions(), arguments, new Interceptor(interceptor));
    }
    catch (TypeLoadException e)
    {
        throw new ArgumentException(Resources.InvalidMockClass, e);
    }
    catch (MissingMethodException e)
    {
        throw new ArgumentException(Resources.ConstructorNotFound, e);
    }
}
于 2012-10-08T21:55:55.090 回答