Default value ILookup<> in loose mode is `null`
Based on wiki, and as ILookup<> inherits from IEnumerable<>, I would expect the default value in loose mode to be an empty lookup, not null.
returns default values or empty arrays, enumerables, etc
Am I missing something here?
Hi @ggirard07, IIRC the code behind DefaultValue.Empty performs a strict type check and doesn't take type inheritance into account.
Before you ask: yes, of course this could be changed. We've had similar requests in the past. (If I find the time, I'll link to some of them later.) However, I have been hesitant to change the behavior of DefaultValue.Empty because it is a core part of Moq and functional changes to it it could affect much pre-existing user code.
Now that Moq vNext has been shelved (for the time being) and we're free to do a new major version, perhaps we'll be able to change that behavior in v5.
The following (partially) works as long as the signature of the method being invoked clearly uses ILookup<> (i.e. won't work with IServiceProvider since the return type isn't known):
using Moq;
using Xunit;
var mock = new Mock<ILookupProvider>();
mock.SetReturnsDefault(new Dictionary<string, string>().ToLookup(x => x.Key, x => x.Value));
var lookup = mock.Object.GetLookup<string, string>();
Assert.NotNull(lookup);
public interface ILookupProvider
{
ILookup<TKey, TValue> GetLookup<TKey, TValue>();
}