Moq - Setup return values of strongly typed class

c#, mocking, moq, unit-testing

Solution

BTW: Is `IParameters.Types` a property or a method?

You could just create a stub for the `IParameter` parameter, since you propably won't want to setup each method of IList:

class ParameterStub : List<String>, IParameter<String> { }

Then, mocking `IParameters` would be as simple as this:

...
var mock = new Mock<IParameters>();
mock.Setup(x => x.Types).Returns(new ParameterStub() {"1","2"});
var m = new MyClass();
var result = m.GetResult(mock.Object);
...

Problem

been trying to make a unit test using moq. here's a class structure. ``` public interface IParameter<T> : IList<T> { } public interface IParameters { IParameter<string> Types; } public class MyClass { public DataTable GetResult(IParameters parameters) { //do work here return dt; } } class TestMyClass { [Test] public void TestGetResult() { var _mock = new Mock<IParameters>(); _mock.SetUp(x => x.Types).Return(new [] {"1", "2"}); //Issue var m = new MyClass() var result = m.GetResult(_mock.Object) Assert.IsNotNull(result); } } ``` i need to learn how to call _mock.SetUp properly so the property in IParameters return an IList type. Alternately, I also tried this... ``` var mock = new Mock<IParameters>(); var mockparams = new Mock<IParameter<string>>(); mockparams.SetReturnsDefault( ); //What should i call to add { "1", "2", "3" } mock.Setup(x => x.ReportTypes).Returns(mockparams.Object); ``` so in mockparams what function should i call to set the return values?

Original source