NMock 列表问题

发布于 2024-08-14 12:32:43 字数 2131 浏览 9 评论 0 原文

我使用 NMock 进行了以下测试,但失败了。过去,当服务调用的结果传递到视图时,它可以工作,但现在由于结果被转换为 dto,所以它失败了。

我认为这可能意味着我需要创建一个自定义匹配器,但我不确定。有人有什么想法吗?

错误消息:

Test method Dgc.Cpo.RM.UI.Presentation.Test.ProjectPresenterTest.LoadProjectsTest threw exception:  NMock2.Internal.ExpectationException: unexpected invocation of projectView.SetProjects(<System.Collections.Generic.List`1[Dgc.Cpo.ResourceManagement.UI.Presentation.ProjectPresentationDto]>)

预期: 1 次:projectView.SetProjects(等于 ) [调用 0 次] 。

代码:

public void LoadProjectsTest()
    {
        IList<ProjectServiceGetProjectsResponse> expectedGetProjectsResponse = new List<ProjectServiceGetProjectsResponse>();
        expectedGetProjectsResponse.Add(new ProjectServiceGetProjectsResponse() { Id = 1, Name = "Project 1", RefNo = "001", Complete = false, Dropped = false });
        expectedGetProjectsResponse.Add(new ProjectServiceGetProjectsResponse() { Id = 2, Name = "Project 2", RefNo = "002", Complete = true, Dropped = false });
        expectedGetProjectsResponse.Add(new ProjectServiceGetProjectsResponse() { Id = 3, Name = "Project 3", RefNo = "003", Complete = false, Dropped = true });

        IList<ProjectPresentationDto> expectedSetProjectsRequest = new List<ProjectPresentationDto>();
        expectedSetProjectsRequest.Add(new ProjectPresentationDto(expectedGetProjectsResponse[0]));
        expectedSetProjectsRequest.Add(new ProjectPresentationDto(expectedGetProjectsResponse[1]));
        expectedSetProjectsRequest.Add(new ProjectPresentationDto(expectedGetProjectsResponse[2]));

        ProjectPresenter_Accessor target = new ProjectPresenter_Accessor(this.mockView,
                                                                         this.mockProjectService);

        Expect.Once.On(this.mockProjectService).Method("GetProjects").Will(Return.Value(expectedGetProjectsResponse));
        Expect.Once.On(this.mockView).Method("SetProjects").With(expectedSetProjectsRequest);

        target.LoadProjects();

        this.mock.VerifyAllExpectationsHaveBeenMet();

    }

I have the following test using NMock which fails. It used to work when the result from the service call was passed to the view but it now fails since the results are converted to dto's.

I think this might mean I need to create a custom matcher but I am not sure. Does anyone have any ideas?

Error Message:

Test method Dgc.Cpo.RM.UI.Presentation.Test.ProjectPresenterTest.LoadProjectsTest threw exception:  NMock2.Internal.ExpectationException: unexpected invocation of projectView.SetProjects(<System.Collections.Generic.List`1[Dgc.Cpo.ResourceManagement.UI.Presentation.ProjectPresentationDto]>)

Expected:
1 time: projectView.SetProjects(equal to ) [called 0 times]
.

Code:

public void LoadProjectsTest()
    {
        IList<ProjectServiceGetProjectsResponse> expectedGetProjectsResponse = new List<ProjectServiceGetProjectsResponse>();
        expectedGetProjectsResponse.Add(new ProjectServiceGetProjectsResponse() { Id = 1, Name = "Project 1", RefNo = "001", Complete = false, Dropped = false });
        expectedGetProjectsResponse.Add(new ProjectServiceGetProjectsResponse() { Id = 2, Name = "Project 2", RefNo = "002", Complete = true, Dropped = false });
        expectedGetProjectsResponse.Add(new ProjectServiceGetProjectsResponse() { Id = 3, Name = "Project 3", RefNo = "003", Complete = false, Dropped = true });

        IList<ProjectPresentationDto> expectedSetProjectsRequest = new List<ProjectPresentationDto>();
        expectedSetProjectsRequest.Add(new ProjectPresentationDto(expectedGetProjectsResponse[0]));
        expectedSetProjectsRequest.Add(new ProjectPresentationDto(expectedGetProjectsResponse[1]));
        expectedSetProjectsRequest.Add(new ProjectPresentationDto(expectedGetProjectsResponse[2]));

        ProjectPresenter_Accessor target = new ProjectPresenter_Accessor(this.mockView,
                                                                         this.mockProjectService);

        Expect.Once.On(this.mockProjectService).Method("GetProjects").Will(Return.Value(expectedGetProjectsResponse));
        Expect.Once.On(this.mockView).Method("SetProjects").With(expectedSetProjectsRequest);

        target.LoadProjects();

        this.mock.VerifyAllExpectationsHaveBeenMet();

    }

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

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

发布评论

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

评论(1

雨后咖啡店 2024-08-21 12:32:43

您已经知道了 - 您需要创建一个自定义匹配器和一个静态工厂类,如 NMock 2.0 高级主题文档.

默认情况下,NMock 使用 Equals 来匹配传递给期望的参数(在您的情况下,查看 LoadProjects 是否将 mockView.Projects 设置为 是否预期SetProjectsRequest )。

List.Equals 实现(继承自 Object)只是 检查两个变量是否引用同一个对象,在本例中显然没有。 (它看起来像 LoadProjects 方法在将 get 访问器结果转换为 DTO 的过程中创建一个新的 DTO 列表实例。)

您必须告诉 NMock 做什么,而不是检查引用相等性。幸运的是,我上面链接的 NMock 文档中的自定义 ListMatcher (以及相应的 IsList 类)可以自行完成此操作。如果 ProjectPresentationDto.Equals 检查值是否相等,则可以按原样使用该示例类。如果没有,您必须对其进行自定义以定义 DTO 的相等性。

You've got it - you need to create a custom matcher and a static factory class, as described in the "Customer Matchers in NMock 2.0" section of the NMock 2.0 advanced topics documentation.

By default, NMock uses Equals to match arguments passed to expectations (in your case, to see whether LoadProjects sets mockView.Projects to expectedSetProjectsRequest or not).

The List<T>.Equals implementation (inherited from Object) just checks whether the two variables reference the same object, and in this case they clearly do not. (It looks like the LoadProjects method creating a new DTO list instance in the process of transforming the get accessor results into DTOs.)

You have to tell NMock what to do instead of checking reference equality. Luckily the custom ListMatcher (and corresponding IsList class) from the NMock documentation I linked above may do the trick all by itself. If ProjectPresentationDto.Equals checks for value equality, you can use that example class as-is. If not, you'll have to customize it to define equality for your DTOs.

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