Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq, SetupGet, Mocking a property

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

Mocking read-only properties means properties with getter method only.

Note that you should declare it as virtual, otherwise System.NotSupportedException will be thrown.

If you're using an interface, that does not apply to you. It works instantly because the mock framework will implement the interface on the fly for you.