1

I found the post titled MOQ: Returning value that was passed into a method which was quite helpful in that I can get one of the values passed in to provide as a return value.

The issue I am faced with is that I need to convert the value (a string) to an IEnumerable as the one and only item in the enumeration, and I am getting tripped up on the syntax for doing so.

Here is what I have so far:

var aggregator = new Mock<IUrlAggregator>();
        aggregator
            .Setup(x => x.ProcessUrl(It.IsAny<IUrlFileLineInfo>()))
            .Returns((IUrlFileLineInfo x) => x.Url); <-- Need to return IEnumerable<string> here, not string

The ProcessUrl signature looks like this:

IEnumerable<string> ProcessUrl(IUrlFileLineInfo urlInfo);

And the IUrlFileLineInfo interface:

public interface IUrlFileLineInfo
{
    string Url { get; set; }
    string Mode { get; set; }
}

So how can I convert the string value to an IEnumerable within the context of a lamba expression (through the .Returns() method)?

Update

This is what I ended up with. Thanks for the help. Hopefully this will help others who are new to lambdas.

var aggregator = new Mock<IUrlAggregator>();
        aggregator
            .Setup(x => x.ProcessUrl(It.IsAny<IUrlFileLineInfo>()))
            .Returns((IUrlFileLineInfo x) => new List<string>() { x.Url });
Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212

1 Answers1

1

What about returning an array with the one item x.Url ?

var aggregator = new Mock<IUrlAggregator>();
        aggregator
            .Setup(x => x.ProcessUrl(It.IsAny<IUrlFileLineInfo>()))
            .Returns((IUrlFileLineInfo x) => new [] { x.Url });
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • Ahh, you beat me to it. I just tried a similar solution and it worked. It didn't dawn on me until after I posted this to declare the array after the =>. – NightOwl888 Oct 09 '12 at 20:09