Moq,SetupGet,嘲弄一个属性

Han*_*del 76 c# properties moq c#-4.0

我正在尝试模拟一个名为的类,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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个演示者课程:

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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了以下测试,我在这里模拟实体,尝试获取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);
    }
Run Code Online (Sandbox Code Playgroud)

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

System.Func<System.Collection.Generic.List<string>>
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

nem*_*esv 159

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

但是对于这条线,你只想回归 string

input.SetupGet(x => x.ColumnNames).Returns(temp[0]);
Run Code Online (Sandbox Code Playgroud)

这导致了异常.

将其更改为返回整个列表:

input.SetupGet(x => x.ColumnNames).Returns(temp);
Run Code Online (Sandbox Code Playgroud)

  • SetupGet()就是我想要的.谢谢! (14认同)
  • 看来我需要休息了。非常感谢您的帮助!(+1 n 将在 7 分钟内接受您的回答) (3认同)

小智 5

模拟只读属性意味着仅具有 getter 方法的属性。

请注意,您应该将其声明为virtual,否则System.NotSupportedException将被抛出。

如果您使用的是接口,则这不适用于您。它立即起作用,因为模拟框架将为您即时实现接口。

  • 仅出于可测试性而将属性虚拟化让我感到困扰。您应该能够阅读代码并深入了解设计。为了可测试性而必须猜测某些东西是否是虚拟的很烦人。 (2认同)