简体   繁体   English

如何在C#中对firstordefault()进行单元测试?

[英]How to unit test firstordefault() in c#?

I want to mock the below line of code(by MOQ in C#,MVC) :- 我想模拟下面的代码行(通过C#,MVC中的MOQ):

CustomerDto target = CustomerService.GetAllCustomer().FirstOrDefault(n => n.CustomerID == customer.CustomerID);

Where CustomerService.GetAllCustomer() function is the dependency in the controller method. 其中CustomerService.GetAllCustomer()函数是控制器方法中的依赖项。

Where it is using FirstOrDefault() function.And in unit testing i have no clue how to mock it. 它在哪里使用FirstOrDefault()函数。在单元测试中,我不知道如何模拟它。

Can any one suggest me way for it ? 有人可以建议我这样做吗?

Mock(stub) your dependency only . 仅模拟(依赖)您的依赖项 In this case it is CustomerService, which should be some interface or abstract class implementation. 在这种情况下,它是CustomerService,它应该是某些接口或抽象类的实现。 Make your GetAllCustomer method return some fake customers. 使您的GetAllCustomer方法返回一些假顾客。 FirstOrDefault is a .NET Framework method which should not be tested (it is already tested by the framework developers) FirstOrDefault是一个.NET Framework方法,不应对其进行测试(框架开发人员已经对其进行了测试)

IMO, you need to start decoupling your code by moving towards a more layered approach. IMO,您需要通过采用更分层的方法来开始分离代码。 I am not quite sure what you need to achieve by mocking the FirstOrDefault method. 我不太确定您需要通过模拟FirstOrDefault方法来实现什么。 I suggest having three layers and their tests as below - 我建议进行三层测试,如下所示-

Data access - uses EF and its DB context and implements a data access interface. 数据访问-使用EF及其数据库上下文并实现数据访问接口。 You should unit test this without mocking EF's db context. 您应该对此单元进行测试,而不模拟EF的数据库上下文。 Tests for this layer would be "state" dependent. 此层的测试将取决于“状态”。 By that I mean, your tests will work with real data and database for the CRUD operations. 我的意思是,您的测试将使用真实的数据和数据库进行CRUD操作。 You just need to make sure you do not persist the changes to the db after the test run. 您只需要确保在测试运行后不要将更改持久保存到数据库。 One can use Spring.Net's testing library to achieve this or simply run your tests under a transaction scope and roll back the transaction after every test run (in the clean up). 可以使用Spring.Net的测试库来实现此目的,或者仅在事务范围内运行测试,然后在每次测试运行后回滚事务(在清理过程中)。

Business logic - contains the business logic and works with the data access interface. 业务逻辑-包含业务逻辑并与数据访问接口一起使用。 Use any DI framework like spring.net or ms unity to inject the data access layer. 使用任何DI框架(例如spring.net或ms unity)注入数据访问层。 You should unit test this by trying to avoid actual database calls. 您应该通过避免实际的数据库调用来对此进行单元测试。 This is where something like a NMock, Rhinomock or MOQ comes into picture. 这是NMock,Rhinomock或MOQ出现的地方。 Set up boundary and exception conditions using mocks and make sure your business logic addresses all the concerns. 使用模拟设置边界条件和异常条件,并确保您的业务逻辑解决了所有问题。

MVC Controller - Will delegate the calls to business logic layer and most probably handle stuff like notifications etc needed on the UI. MVC控制器-将调用委派给业务逻辑层,最有可能处理UI上所需的内容,如通知等。 It should be a judgement call if the controller really needs to be unit tested. 如果控制器确实需要进行单元测试,那应该是一个判断电话。 It could be an overkill more than often. 它可能比其他情况更容易被滥用。 I would rather suggest considering automated UI test cases using something like selenium or Microsoft's coded UI tests. 我宁愿建议考虑使用硒或Microsoft编码的UI测试之类的自动化UI测试用例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM