在单位测试中包括基于事件的模型
我是C#和.NET开发的新手,并且正在从 thittingeventservice.cs
中测试一种方法。
ThingUpDatedEvent.cs
包括在服务文件中,我正在尝试将其包括在测试中,但是我在1中遇到了麻烦。确保测试已识别为2。确定什么。我需要提供才能正确包括在内。
我遇到以下错误:
models/thingUpdatedEvent.cs(12,15):错误CS1729:'对象'不包含构造函数,该构造函数
,该构造函数指向:base>:base()
ThingUpdatedEvent.cs
。
如果我从测试中删除以下内容:
ThingUpdatedEvent request = new ThingUpdatedEvent
(updateRequest, thingBeforeUpdate, thingAfterUpdate)
{
UpdateRequest = UpdateThingRequest,
ThingBeforeUpdate = ThingBeforeUpdate,
ThingAfterUpdate = ThingAfterUpdate
};
await this._svc.HandleThingUpdatedAsync(request);
然后测试运行,但是我得到了 soq.mockexception
错误,说明它“预期一次在模拟上调用一次,但为0次”。这使我认为我需要包括 thingUpdatedEvent
,以便测试运行和通过。
问题:
- 我是否需要在测试中包括
thitupDatedEvent
?- 如果没有,什么可能导致调用不运行?
-
是:base()
在基于事件的模型上允许吗?
ThingUpDatedEvent.cs
namespace someFolder.someOtherFolder.etc
{
public class ThingUpdatedEvent
{
public ThingUpdatedEvent(
[NotNull] UpdateThingRequest updateRequest,
[NotNull] Thing thingBeforeUpdate,
[NotNull] Thing thingAfterUpdate)
: base(updateRequest, thingBeforeUpdate, thingAfterUpdate) // is base:() necessary for this?
{
this.UpdateRequest = updateRequest;
this.ThingBeforeUpdate = thingBeforeUpdate;
this.ThingAfterUpdate = thingAfterUpdate;
}
[NotNull]
public UpdateThingRequest UpdateRequest { get; set; }
[NotNull]
public Thing ThingBeforeUpdate { get; set; }
[NotNull]
public Thing ThingAfterUpdate { get; set; }
}
}
ThingEventservicEtests.cs
public class ThingEventServiceTests
{
Mock<IAuditLog> _auditLog;
Mock<AuditHelper> _auditHelper;
ThingEventService _svc;
public ThingEventServiceTests()
{
this._auditLog = new Mock<IAuditLog>();
this._auditHelper = new Mock<AuditHelper>();
this._svc = new ThingEventService(
Mock.Of<IAEService>(),
this._auditLog.Object);
}
[Fact]
public async Task CanHandleThingUpdatedAsync()
{
var SourceEntities = new List<AEModel>
{
AETypes.System.Create("Things")
};
ThingUpdatedEvent request = new ThingUpdatedEvent
(updateRequest, thingBeforeUpdate, thingAfterUpdate)
{
UpdateRequest = UpdateThingRequest,
ThingBeforeUpdate = ThingBeforeUpdate,
ThingAfterUpdate = ThingAfterUpdate
};
await this._svc.HandleThingUpdatedAsync(request);
It.IsAny<IEnumerable<AEModel>>();
this._auditLog.Verify(m => m.RecordEventAsync(
"123435",
"requester",
SourceEntities,
AuditEventTypes.ThingEdited,
"Thing edited",
"Thing was edited: Some Title",
It.IsAny<IEnumerable<FieldChange>>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<IEnumerable<Tag>>(),
null,
null,
null
), Times.Once);
}
}
ThingEventservice.cs
public async task HandleThingUpdatedAsync(ThingUpdatedEvent evt)
{
// if (condition)
{
IEnumerable<AEModel> defaultSourceEntities = await this._auditHelper
.GetDefaultSourceEntitiesForThingEventAsync(evt.ThingAfterUpdate);
Task auditLogEditTask = this._auditLog.RecordEventAsync(
id: evt.ThingAfterUpdate.Id.ToString(),
user: evt.UpdateRequest.Requester,
sourceEntities: defaultSourceEntities,
eventType: AuditEventTypes.ThingEdited,
title: "Thing edited",
description: $"Thing was edited: {evt.ThingAfterUpdate.Title} (ID: {evt.ThingAfterUpdate.Id})",
fieldChanges: fieldChanges,
comments: null,
tags: this._auditHelper.GetDefaultTagsForThing(evt.ThingAfterUpdate));
tasks.Add(auditLogEditTask);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就可以做到
更新:我无需更新
thitupDatedEvent.cs
。感谢@ralf指出了基类的问题,并指向我朝正确的方向指导我。Update: I was able to do
without having to update
ThingUpdatedEvent.cs
. Thanks @ralf for pointing out the issue with the base class and for steering me in the right direction.