【发布时间】:2016-09-20 01:24:10
【问题描述】:
如何编写转义或绕过正在测试的大方法中的少数方法和条件的测试。
比如我下面有这个方法:
public bool IsValid(int id)
{
var details = _myService.GetDetails(id); // This line should be avoided in test
var doctorDetails = _myService.GetDoctorDetails("AUS"); // This needs to be executed
if(details.Name == "Ab") // This if I dont want to be part of my test
{
// Do something
}
if(doctorDetails !=null )
{
// Code to test
}
}
编辑了我的代码,我只需要在我的方法中测试第二个 IF 语句。如上所述,我需要对医生详细信息的值进行硬编码以从服务中获取其数据,因为为了从服务中获取它,应该存在详细信息,但事实并非如此。否则,唯一的选择是模拟它并将详细信息存储在数据库中并调用此服务,我暂时不想这样做
【问题讨论】:
-
@GrantWinney:我是测试新手,是的,我想,如何为我的需求编写测试,请帮我写代码
-
@GrantWinney:我的简单问题是,如何硬编码医生详细信息 ratehr 的 bvalue,而不是从服务中获取它,然后只测试第二个 if 语句。 Doctordetails 只是一个字符串值
标签: c# unit-testing mocking nunit rhino-mocks