Moq, SetupGet, Mocking a property


99

Próbuję mockować klasę, nazwaną UserInputEntity, która zawiera właściwość o nazwie ColumnNames: (zawiera inne właściwości, właśnie uprościłem to do pytania)

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

Mam klasę prezentera:

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

Wypróbowałem następujący test, w którym kpię z jednostki, próbuję uzyskać ColumnNameswłaściwość, aby zwrócić zainicjowaną, List<string>()ale nie działa:

    [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

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

Any help would be appreciated.

Odpowiedzi:


200

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

But with this line you are trying to return just a 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);

3
Looks like i need a break. Thanks very much for your help! (+1 n will accept ur ans in 7mins)
Hans Rudel

18
SetupGet() was what I was looking for. Thanks!
imnk

Same as me, used SetUpGet() for the class property and it works.
hussian

4

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.


1
So how does one declare a read-only property as virtual in an interface?
erict
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.