C# Moq、SetupGet、Mocking 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12141799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:14:13  来源:igfitidea点击:

Moq, SetupGet, Mocking a property

c#c#-4.0propertiesmoq

提问by Hans Rudel

I'm trying to mock a class, called UserInputEntity, which contains a property called ColumnNames: (it does contain other properties, I've just simplified it for the question)

我正在尝试模拟一个名为 的类,UserInputEntity其中包含一个名为ColumnNames:的属性(它确实包含其他属性,我只是针对问题对其进行了简化)

namespace CsvImporter.Entity
{
    public interface IUserInputEntity
    {
        List<String> ColumnNames { get; set; }
    }

    public class UserInputEntity : IUserInputEntity
    {
        public UserInputEntity(List<String> columnNameInputs)
        {
            ColumnNames = columnNameInputs;
        }

        public List<String> ColumnNames { get; set; }
    }
}

I have a presenter class:

我有一个演讲者课程:

namespace CsvImporter.UserInterface
{
    public interface IMainPresenterHelper
    {
        //...
    }

    public class MainPresenterHelper:IMainPresenterHelper
    {
        //....
    }

    public class MainPresenter
    {
        UserInputEntity inputs;

        IFileDialog _dialog;
        IMainForm _view;
        IMainPresenterHelper _helper;

        public MainPresenter(IMainForm view, IFileDialog dialog, IMainPresenterHelper helper)
        {
            _view = view;
            _dialog = dialog;
            _helper = helper;
            view.ComposeCollectionOfControls += ComposeCollectionOfControls;
            view.SelectCsvFilePath += SelectCsvFilePath;
            view.SelectErrorLogFilePath += SelectErrorLogFilePath;
            view.DataVerification += DataVerification;
        }


        public bool testMethod(IUserInputEntity input)
        {
            if (inputs.ColumnNames[0] == "testing")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

I've tried the following test, where I mock the entity, try to get ColumnNamesproperty to return a initialized List<string>()but it's not working:

我尝试了以下测试,在其中模拟实体,尝试获取ColumnNames属性以返回已初始化List<string>()但它不起作用:

    [Test]
    public void TestMethod_ReturnsTrue()
    {
        Mock<IMainForm> view = new Mock<IMainForm>();
        Mock<IFileDialog> dialog = new Mock<IFileDialog>();
        Mock<IMainPresenterHelper> helper = new Mock<IMainPresenterHelper>();

        MainPresenter presenter = new MainPresenter(view.Object, dialog.Object, helper.Object);

        List<String> temp = new List<string>();
        temp.Add("testing");

        Mock<IUserInputEntity> input = new Mock<IUserInputEntity>();

    //Errors occur on the below line.
        input.SetupGet(x => x.ColumnNames).Returns(temp[0]);

        bool testing = presenter.testMethod(input.Object);
        Assert.AreEqual(testing, true);
    }

The errors I get state that there are some invalid arguments + Argument 1 cannot be converted from string to

我得到的错误表明存在一些无效参数 + 参数 1 无法从字符串转换为

System.Func<System.Collection.Generic.List<string>>

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by nemesv

ColumnNamesis a property of type List<String>so when you are setting up you need to pass a List<String>in the Returnscall as an argument (or a func which return a List<String>)

ColumnNames是一个类型的属性,List<String>所以当你设置时,你需要List<String>Returns调用中传递 a作为参数(或返回 a 的 func List<String>

But with this line you are trying to return just a string

但是通过这条线,你试图返回一个 string

input.SetupGet(x => x.ColumnNames).Returns(temp[0]);

which is causing the exception.

这导致了异常。

Change it to return whole list:

更改它以返回整个列表:

input.SetupGet(x => x.ColumnNames).Returns(temp);

回答by Himanshu Soni

But while mocking read-only properties means properties with getter method only you should declare it as virtual otherwise System.NotSupportedException will be thrown because it is only supported in VB as moq internally override and create proxy when we mock anything.

但是,虽然模拟只读属性意味着仅具有 getter 方法的属性,您应该将其声明为虚拟,否则将抛出 System.NotSupportedException ,因为它仅在 VB 中受支持,因为当我们模拟任何内容时,moq 内部覆盖并创建代理。