简体   繁体   English

单元测试假存储库,如何在不先添加实体的情况下测试GetById方法?

[英]Unit testing fake repository, how can I test the GetById method without first adding an entity?

My understanding is that you have to write unit tests that isolate functionality. 我的理解是你必须编写隔离功能的单元测试。 So given a repository class that has this method: 所以给定一个具有此方法的存储库类:

Entity GetById(Guid id)

and a fake implementation (using a Dictionary for storage), how would you write a test without first adding an entity? 和一个虚假的实现(使用字典存储),如果没有先添加实体,你会如何编写测试? Is it ok to use a known set of guids for testing? 是否可以使用一组已知的guid进行测试? Then in the fake repository constructor I could fill the dictionary with a couple of entities where the guids follow a pattern, so that I can test the GetById() method with a guid that I know will return an entity. 然后在假存储库构造函数中,我可以用一些guid遵循模式的实体填充字典,这样我就可以使用guid测试GetById()方法,我知道它会返回一个实体。

Thanks for reading! 谢谢阅读!

PS. PS。 This is my first time writing unit tests. 这是我第一次编写单元测试。

Yes, you could use known test ids in your test - that is what I would do. 是的,您可以在测试中使用已知的测试ID - 这就是我要做的。 Although I have become a fan of Rhino Mocks which lets you put more directly in the test about what you'd expect to the mock object to do. 虽然我已成为Rhino Mocks的粉丝,但是你可以让你更直接地测试你对模拟对象的期望。

For example just before your call to the repository, you would do this: 例如,在您调用存储库之前,您将执行以下操作:

Expect.Call(repository.GetById("someObject")).Return(new RepositoryThing());

It appeals to me anyway :) 无论如何它吸引我:)

Yes, using a fake implementation of an object/interface with a fixed list of items that can be queried from the fake instance is a valid practice. 是的,使用伪造的对象/接口实现以及可以从伪实例查询的固定项目列表是一种有效的做法。

Obviously, without adding an entry first, one can only test what's returned when the Guid can't be found in the repository. 显然,如果不首先添加条目,则只能测试在存储库中找不到Guid时返回的内容。

In C# allows it, one also can have the fake implementation of the repository have a method to add items to the repository. 在C#中允许它,一个也可以有虚假实现的存储库有一个方法来向存储库添加项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何对使用Entity Framework实现的存储库方法进行单元测试? - How should I unit test a repository method implemented with Entity Framework? 我如何单元测试实体框架代码优先映射? - How can I unit test Entity Framework Code First Mappings? 如何伪造html内容以进行单元测试的方法 - how to fake html content to unit test a method 如何正确地对与Entity Framework / DbContext耦合的存储库模式方法进行单元测试? - How do I properly unit test a repository-pattern method, that has coupling to Entity Framework / DbContext? 如何在单元测试中测试方法 - How to test method in unit testing 当我使用工作单元和存储库模式时,如何从服务测试方法 - how can I test method from service when I use unit of work and repository pattern 单元测试:如何测试通用存储库 - Unit testing: How to test generic repository 如何使用存储库模式在实体框架中伪造 DbContext.Entry 方法 - How to fake DbContext.Entry method in Entity Framework with repository pattern 如何在 C# 单元测试中伪造内置方法的返回? - How do I fake the return of a built-in method in C# unit test? 如何伪造HttpContext进行单元测试? - How can I fake HttpContext for unit tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM