为什么在调试移动到数据访问层时仍然没有模拟单元测试

Why unit tests are still not mocked as when debugging its moving to data access layes

var ClosedLoopTrxSyncDAL = new Mock<ITrxSyncDAL<DeviceTrxSyncTracking>>();
 Mock<IOptions<AppSettings>> appSettings = new Mock<IOptions<AppSettings>>();
 var dt = new DataTable();

 var isValid = true;
DeviceTrxSyncTracking transactionItems = new DeviceTrxSyncTracking()
List<ClosedLoopTrxItem> ClosedLoopTrxList = new List<ClosedLoopTrxItem>();   
ClosedLoopTrxSyncDAL.Setup(post => post.InsertTransaction(dt, transactionItems)).Returns(isValid);
var controller = new ClosedLoopSyncController(appSettings.Object);
SyncDataAnchor result = controller.ClosedLoopSyncService(ClosedLoopTrxList);

即使在模拟它并 returning 我自己的对象之后,调试器也会移动到数据访问层。预期:当我调试测试时,它不应该从业务层移动到数据访问层,而应该 return 一个现成的对象

当有人试图访问设置时,您需要告诉模拟对象 return 什么。例如,如果您的 AppSettings class 看起来像这样:

public class AppSettings
{
    public string MyValue { get; set; }
}

在上面的测试代码中,您可以创建一个 AppSettings 对象并填充您想要的值。例如:

 Mock<IOptions<AppSettings>> appSettings = new Mock<IOptions<AppSettings>>();

 var testAppSettings = new AppSettings { MyValue = "This is a test" };
 appSettings.SetupGet(a => a.Value).Returns(testAppSettings);

现在,当您的控制器 class 读取 MyValue 属性 时,它将获得字符串 "This is a test".