Moq, SetupGet, Mocking

I am trying to make fun of a class called UserInputEntity that contains a property called ColumnNames : (it contains other properties, I just simplified it for the question)

 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 tried the following test, where I mock the entity, try to get the ColumnNames property to return the initialized List<string>() , but it does not work:

  [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 claim 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.

+51
c # properties moq
Aug 27 '12 at 12:14
source share
2 answers

ColumnNames is a property of type List<String> , so when setting up, you need to pass List<String> in the Returns call as an argument (or func, which returns List<String> )

But with this string you are trying to return only string

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

which throws an exception.

Change it to return the whole list:

 input.SetupGet(x => x.ColumnNames).Returns(temp); 
+121
Aug 27 '12 at 12:17
source share
— -

But while mocking read-only properties means properties only with the getter method, you must declare it virtual, otherwise a System.NotSupportedException will be thrown, because it is only supported in VB as an internal moq override and creates a proxy when we- then make fun of.

+3
May 19 '18 at 18:34
source share



All Articles