我的測試案例總是失敗,請告訴我,我錯了,代碼如下C#單元測試嘲諷使用Moq.dll
public class EmployeeService
{
private readonly IRepository _repository;
public EmployeeService(IRepository repository)
{
_repository = repository;
}
public bool SaveEmployeeData(Employee employee,bool isSaveNeeded)
{
bool result = false;
try
{
if (isSaveNeeded)
{
result= _repository.Save(employee);
}
}
catch (Exception ex)
{
throw new Exception();
}
return result;
}
}
和我的測試用例是
[TestMethod()]
public void SaveEmployeeDataTest()
{
var a = new Mock<IRepository>();
a.Setup(s => s.Save(new Employee())).Returns(true).Verifiable();
var result = new EmployeeService(a.Object).SaveEmployeeData(new Employee(), true);
Assert.IsTrue(result);
}
它總是失敗。
*它失敗了嗎? (爲什麼你有一個catch塊,它只丟失信息,爲什麼你不想拋出原來的異常呢?) –