简体   繁体   English

使用 Moq 模拟接口属性

[英]Mock interface properties using Moq

I have the following interface:我有以下界面:

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

I'm trying to mock it in such a way that GetCurrentHealthStatus is true .我试图以GetCurrentHealthStatustrue的方式来模拟它。

I thought this should have worked but it doesn't:我认为这应该有效,但它没有:

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

How is this done?这是怎么做的?

Have a look at https://github.com/Moq/moq4/wiki/Quickstart看看https://github.com/Moq/moq4/wiki/Quickstart

try this尝试这个

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

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

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