简体   繁体   English

Moq,SetupGet,模拟属性

[英]Moq, SetupGet, Mocking a property

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 ColumnNames property 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. 任何帮助,将不胜感激。

ColumnNames is a property of type List<String> so when you are setting up you need to pass a List<String> in the Returns call as an argument (or a func which return a List<String> ) ColumnNamesList<String>类型的属性,因此在设置时,需要在Returns调用中传递List<String>作为参数(或返回List<String>的func)。

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);

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

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

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