测试订阅 EventAggregator
Test subscription to EventAggregator
感谢这个 SO answer 我已经成功地测试了 PRISM EventAggregator 的发布事件(使用 FakeItEasy 伪造)
[TestCase]
public void test_that_publish_occured()
{
var fakeEventAg = A.Fake<IEventAggregator>();
var fakeEvent = A.Fake<MyEvent>();
A.CallTo(() => fakeEventAg.GetEvent<MyEvent>())
.Returns(fakeEvent);
MyViewModel mvm = new MyViewModel(fakeEventAg);
mvm.ICommandThatCausesPublishToBeCalled.Execute();
A.CallTo(() => fakeEvent.Publish(A<SomeClass>.Ignored))
.MustHaveHappened();
}
但是我没有测试订阅这个事件。我尝试了以下方法,但出现异常 "Non virtual methods can not be intercepted".
[TestCase]
public void test_that_event_is_listened()
{
var fakeEventAg = A.Fake<IEventAggregator>();
var fakeEvent = A.Fake<MyEvent>();
A.CallTo(() => fakeEventAg.GetEvent<MyEvent>())
.Returns(fakeEvent);
// subscription occurs in the constructor
MyViewModel2 mvm2 = new MyViewModel2(fakeEventAg);
A.CallTo(() => fakeEventAg.GetEvent<MyEvent>()
.Subscribe(A<Action<PayloadClass>>.Ignored))
.MustHaveHappened();
}
如何测试事件订阅是否已发生?不一定是单元测试,也可以是集成测试。
错误发生是因为 Subscribe
方法不是虚拟的,因此不能被模拟。我看到 EventBase
class(MyEvent
基于)有一个 Subscriptions
属性。您应该能够检查该集合以验证您的活动是否已添加。
感谢这个 SO answer 我已经成功地测试了 PRISM EventAggregator 的发布事件(使用 FakeItEasy 伪造)
[TestCase]
public void test_that_publish_occured()
{
var fakeEventAg = A.Fake<IEventAggregator>();
var fakeEvent = A.Fake<MyEvent>();
A.CallTo(() => fakeEventAg.GetEvent<MyEvent>())
.Returns(fakeEvent);
MyViewModel mvm = new MyViewModel(fakeEventAg);
mvm.ICommandThatCausesPublishToBeCalled.Execute();
A.CallTo(() => fakeEvent.Publish(A<SomeClass>.Ignored))
.MustHaveHappened();
}
但是我没有测试订阅这个事件。我尝试了以下方法,但出现异常 "Non virtual methods can not be intercepted".
[TestCase]
public void test_that_event_is_listened()
{
var fakeEventAg = A.Fake<IEventAggregator>();
var fakeEvent = A.Fake<MyEvent>();
A.CallTo(() => fakeEventAg.GetEvent<MyEvent>())
.Returns(fakeEvent);
// subscription occurs in the constructor
MyViewModel2 mvm2 = new MyViewModel2(fakeEventAg);
A.CallTo(() => fakeEventAg.GetEvent<MyEvent>()
.Subscribe(A<Action<PayloadClass>>.Ignored))
.MustHaveHappened();
}
如何测试事件订阅是否已发生?不一定是单元测试,也可以是集成测试。
错误发生是因为 Subscribe
方法不是虚拟的,因此不能被模拟。我看到 EventBase
class(MyEvent
基于)有一个 Subscriptions
属性。您应该能够检查该集合以验证您的活动是否已添加。