JustMock 单元测试在它应该失败的时候通过了
JustMock unit test passing when it should fail
我正在使用 JustMock 编写一些单元测试,并且对自己感到非常高兴,因为它们都通过了,直到我试图让一个失败。本单元测试通过:
[TestFixture]
public class TestEventAggregation
{
class BaseEventArgs : EventArgs {}
class DerivedEventArgs : BaseEventArgs {}
class EventAggregationService
{
private Subject<object> _subject = new Subject<object>();
public IDisposable Subscribe<TEventArgs>(EventHandler<TEventArgs> eventHandler)
where TEventArgs : EventArgs
{
return _subject.OfType<EventPattern<TEventArgs>>().Subscribe(
delegate(EventPattern<TEventArgs> pattern)
{
eventHandler(pattern.Sender, pattern.EventArgs);
}
);
}
public void Publish<TEventArgs>(object sender, TEventArgs eventArgs)
where TEventArgs : EventArgs
{
_subject.OnNext(new EventPattern<TEventArgs>(sender, eventArgs));
}
}
[Test]
public void BaseEventIsPublishedToBaseEventSubcriberButNotDerivedEventSubscriber()
{
EventAggregationService eventAggregationService = new EventAggregationService();
// Arrange
Action<object, BaseEventArgs> baseEventHandler = Mock.Create<Action<object, BaseEventArgs>>();
Mock.Arrange(() => baseEventHandler(Arg.AnyObject, Arg.IsAny<BaseEventArgs>())).OccursOnce();
Action<object, DerivedEventArgs> derivedEventHandler = Mock.Create<Action<object, DerivedEventArgs>>();
Mock.Arrange(() => derivedEventHandler(Arg.AnyObject, Arg.IsAny<DerivedEventArgs>())).OccursOnce();
// Act
using (eventAggregationService.Subscribe(new EventHandler<BaseEventArgs>((s, e) => baseEventHandler(s, e))))
using (eventAggregationService.Subscribe(new EventHandler<DerivedEventArgs>((s, e) => derivedEventHandler(s, e))))
{
eventAggregationService.Publish(this, new BaseEventArgs());
}
// Assert
Mock.Assert(baseEventHandler);
Mock.Assert(derivedEventHandler);
}
}
它应该会失败,我在每个 lambda 表达式中放置了一个断点,并且调用了基而不是派生。我已经尝试过移动和扭曲东西,但我终其一生都无法弄清楚为什么这个测试会通过,因为派生的处理程序被确认不会被调用。
要自己构建它,请添加对 nunit、JustMock 和 Rx-Linq NuGet 包的引用。
啊,你发现了一个错误。我刚刚将修复推送到 master。修复将在下一个内部版本中发布。
作为解决方法,您可以显式断言对事件处理程序的调用:
Mock.Assert(() => baseEventHandler(null, null), Args.Ignore(), Occurs.Once());
Mock.Assert(() => derivedEventHandler(null, null), Args.Ignore(), Occurs.Never());
我正在使用 JustMock 编写一些单元测试,并且对自己感到非常高兴,因为它们都通过了,直到我试图让一个失败。本单元测试通过:
[TestFixture]
public class TestEventAggregation
{
class BaseEventArgs : EventArgs {}
class DerivedEventArgs : BaseEventArgs {}
class EventAggregationService
{
private Subject<object> _subject = new Subject<object>();
public IDisposable Subscribe<TEventArgs>(EventHandler<TEventArgs> eventHandler)
where TEventArgs : EventArgs
{
return _subject.OfType<EventPattern<TEventArgs>>().Subscribe(
delegate(EventPattern<TEventArgs> pattern)
{
eventHandler(pattern.Sender, pattern.EventArgs);
}
);
}
public void Publish<TEventArgs>(object sender, TEventArgs eventArgs)
where TEventArgs : EventArgs
{
_subject.OnNext(new EventPattern<TEventArgs>(sender, eventArgs));
}
}
[Test]
public void BaseEventIsPublishedToBaseEventSubcriberButNotDerivedEventSubscriber()
{
EventAggregationService eventAggregationService = new EventAggregationService();
// Arrange
Action<object, BaseEventArgs> baseEventHandler = Mock.Create<Action<object, BaseEventArgs>>();
Mock.Arrange(() => baseEventHandler(Arg.AnyObject, Arg.IsAny<BaseEventArgs>())).OccursOnce();
Action<object, DerivedEventArgs> derivedEventHandler = Mock.Create<Action<object, DerivedEventArgs>>();
Mock.Arrange(() => derivedEventHandler(Arg.AnyObject, Arg.IsAny<DerivedEventArgs>())).OccursOnce();
// Act
using (eventAggregationService.Subscribe(new EventHandler<BaseEventArgs>((s, e) => baseEventHandler(s, e))))
using (eventAggregationService.Subscribe(new EventHandler<DerivedEventArgs>((s, e) => derivedEventHandler(s, e))))
{
eventAggregationService.Publish(this, new BaseEventArgs());
}
// Assert
Mock.Assert(baseEventHandler);
Mock.Assert(derivedEventHandler);
}
}
它应该会失败,我在每个 lambda 表达式中放置了一个断点,并且调用了基而不是派生。我已经尝试过移动和扭曲东西,但我终其一生都无法弄清楚为什么这个测试会通过,因为派生的处理程序被确认不会被调用。
要自己构建它,请添加对 nunit、JustMock 和 Rx-Linq NuGet 包的引用。
啊,你发现了一个错误。我刚刚将修复推送到 master。修复将在下一个内部版本中发布。
作为解决方法,您可以显式断言对事件处理程序的调用:
Mock.Assert(() => baseEventHandler(null, null), Args.Ignore(), Occurs.Once());
Mock.Assert(() => derivedEventHandler(null, null), Args.Ignore(), Occurs.Never());