使用带两个接口的MoqOuter
和Inner
,我无法Outer.Inner.SomeEvent
启动。
public interface Outer
{
Inner Inner { get; }
}
public interface Inner
{
int Prop { get; set; }
event EventHandler PropChanged;
}
public void Test()
{
Mock<Outer> omock = new Mock<Outer>();
Mock<Inner> imock = new Mock<Inner>();
Console.WriteLine("Inner");
imock.Object.PropChanged += InnerPropChanged;
imock.Raise(m => m.PropChanged += null, EventArgs.Empty);
imock.Object.PropChanged -= InnerPropChanged;
// This has no effect.
//omock.Setup(m => m.Inner).Returns(imock.Object);
Console.WriteLine("Outer");
// Both the auto recursive and the explicit above produce the same behavior.
omock.SetupProperty(m => m.Inner.Prop, -1);
omock.Object.Inner.PropChanged += InnerPropChanged;
omock.Raise(m => m.Inner.PropChanged += null, EventArgs.Empty);
}
public void InnerPropChanged(object sender, EventArgs e)
{
Console.WriteLine(" InnerPropChanged");
}
调用时输出Test()
:
Inner
InnerPropChanged
Outer
如何提醒订阅者注意任何Inner
's 事件?似乎没有什么可以解雇他们。
编辑-澄清一下,我希望能够Inner
从上下文中引发事件Outer
,因此最终输出应包括:
Outer
InnerPropChanged