使用 Moq 模拟接口属性

Mock interface properties using Moq

我有以下界面:

public interface IHealthHandler
{
    bool GetCurrentHealthStatus();
    void SetCurrentHealthStatus(bool status);
}

我试图以 GetCurrentHealthStatustrue 的方式来模拟它。

我认为这应该行得通,但行不通:

var mockIHealthHandler = new Mock<IHealthHandler>();
mockIHealthHandler.SetupProperty(x => x.GetCurrentHealthStatus(), true);

这是怎么做到的?

看看https://github.com/Moq/moq4/wiki/Quickstart

试试这个

mockIHealthHandler.Setup(x => x.GetCurrentHealthStatus()).Returns(true);