NMock 列表问题
我使用 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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经知道了 - 您需要创建一个自定义匹配器和一个静态工厂类,如 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 whetherLoadProjects
setsmockView.Projects
toexpectedSetProjectsRequest
or not).The
List<T>.Equals
implementation (inherited fromObject
) just checks whether the two variables reference the same object, and in this case they clearly do not. (It looks like theLoadProjects
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 correspondingIsList
class) from the NMock documentation I linked above may do the trick all by itself. IfProjectPresentationDto.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.