在单位测试中包括基于事件的模型

发布于 2025-01-22 18:43:04 字数 4885 浏览 3 评论 0 原文

我是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);
            }

}

I'm new to C# and .NET development and I'm testing out a method from ThingEventService.cs.

ThingUpdatedEvent.cs is included in the service file and I'm trying to include it in the test, but I'm having trouble with 1. Ensuring that it's recognized by the test, and 2. Determining what I need to provide in order for it to be included properly.

I'm running into the following error:

Models/ThingUpdatedEvent.cs(12,15): error CS1729: 'object' does not contain a constructor that takes 3 arguments

, which is pointing to the : base() line that I've included in ThingUpdatedEvent.cs.

If I remove the following from the test:

ThingUpdatedEvent request = new ThingUpdatedEvent
   (updateRequest, thingBeforeUpdate, thingAfterUpdate)
   {
     UpdateRequest = UpdateThingRequest,
     ThingBeforeUpdate = ThingBeforeUpdate,
     ThingAfterUpdate = ThingAfterUpdate
   };
            
   await this._svc.HandleThingUpdatedAsync(request);

then the test runs, but I'm getting a Moq.Mockexception error stating that it "Expected invocation on the mock once, but was 0 times". It's led me to think that I need to include ThingUpdatedEvent in order for the test to run and pass.


Questions:

  • Do I need to include ThingUpdatedEvent in the test?
    • If not, what could be causing the invocation to not run?
  • Is :base () allowed on event-based models?


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: 
quot;Thing was edited: {evt.ThingAfterUpdate.Title} (ID: {evt.ThingAfterUpdate.Id})",
                    fieldChanges: fieldChanges,
                    comments: null,
                    tags: this._auditHelper.GetDefaultTagsForThing(evt.ThingAfterUpdate));
                tasks.Add(auditLogEditTask);
            }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2025-01-29 18:43:04

就可以做到

        [Fact]
        public async Task CanHandleThingUpdatedAsync()
        {

            UpdateThingRequest updateThingRequest = new UpdateThingRequest(
                something: "something",
                etc: "etc"
            );

            Thing thingBeforeUpdateRequest = new Thing
            {
                Name = "Paper Pusher"
            };

            Thing thingAfterUpdateRequest = new Thing
            {
                Name = "Paper Flinger"
            };

            ThingUpdatedEvent request = new ThingUpdatedEvent(updateThingRequest, thingBeforeUpdateRequest, thingAfterUpdateRequest);
            
            await this._svc.HandleThingUpdatedAsync(request);

// etc

更新:我无需更新 thitupDatedEvent.cs 。感谢@ralf指出了基类的问题,并指向我朝正确的方向指导我。

Update: I was able to do

        [Fact]
        public async Task CanHandleThingUpdatedAsync()
        {

            UpdateThingRequest updateThingRequest = new UpdateThingRequest(
                something: "something",
                etc: "etc"
            );

            Thing thingBeforeUpdateRequest = new Thing
            {
                Name = "Paper Pusher"
            };

            Thing thingAfterUpdateRequest = new Thing
            {
                Name = "Paper Flinger"
            };

            ThingUpdatedEvent request = new ThingUpdatedEvent(updateThingRequest, thingBeforeUpdateRequest, thingAfterUpdateRequest);
            
            await this._svc.HandleThingUpdatedAsync(request);

// etc

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文