You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Firstly, DefaultValue.Empty is described as: "Default behavior, which generates ... empty array and enumerables ...."
It may be a matter of semantics but I expected something like a List<T> to be also treated as "enumerables" in this context. Depending on point of view, applying this to everything implementing IEnumerable<T> may be too much (should a string be "empty" or "null" in this mode?) but how about everything related to ICollection<T>?
If nothing else, I hope this post will help others who encounter the same issue and wonder how to "make it work" (without explicit Setup for each such method on a mock).
Secondly, SetReturnsDefault - it's currently very "picky" ;)
It would be nice, IMHO, if it automatically supported all derived types... and/or wrapped in a Task, ValueTask.
Steps to Reproduce
This (MSTest-style) test code should explain it all:
[TestClass]publicclassDefaultValue_Empty_Enumerables{publicinterfaceIFoo{int[]GetArray();IEnumerable<int>GetIEnumerable();ICollection<int>GetICollection();IList<int>GetIList();List<int>GetList();Task<int>GetArrayAsync();Task<List<int>>GetListAsync();}[TestMethod]publicvoidAllThoseTypesCanBeCalled_Enumerables(){Assert.IsTrue(typeof(int[]).IsAssignableTo(typeof(IEnumerable<int>)));Assert.IsTrue(typeof(ICollection<int>).IsAssignableTo(typeof(IEnumerable<int>)));Assert.IsTrue(typeof(IList<int>).IsAssignableTo(typeof(IEnumerable<int>)));Assert.IsTrue(typeof(List<int>).IsAssignableTo(typeof(IEnumerable<int>)));Assert.IsTrue(typeof(string).IsAssignableTo(typeof(IEnumerable<char>)),"something to watch out for");Assert.IsFalse(typeof(string).IsAssignableTo(typeof(ICollection<char>)),"a more specific workaround?");}[TestMethod]publicvoidUsingDefaultMoqSettings_DefaultValue_Empty(){varmock=newMock<IFoo>();Assert.AreEqual(DefaultValue.Empty,mock.DefaultValue);}[TestMethod]publicasyncTaskMethodsWithTheseReturnTypes_DefaultToEmpty(){varmock=newMock<IFoo>();Assert.IsNotNull(mock.Object.GetArray());Assert.IsNotNull(mock.Object.GetIEnumerable());Assert.IsNotNull(awaitmock.Object.GetArrayAsync());}[TestMethod]publicasyncTaskMethodsWithTheseReturnTypes_DefaultToNull(){varmock=newMock<IFoo>();Assert.IsNull(mock.Object.GetICollection());Assert.IsNull(mock.Object.GetIList());Assert.IsNull(mock.Object.GetList());Assert.IsNull(awaitmock.Object.GetListAsync());}[TestMethod]publicasyncTaskMakingItAllWork_Almost(){varmock=newMock<IFoo>();mock.SetReturnsDefault<ICollection<int>>([]);mock.SetReturnsDefault<IList<int>>([]);mock.SetReturnsDefault<List<int>>([]);Assert.IsNotNull(mock.Object.GetICollection());Assert.IsNotNull(mock.Object.GetIList());Assert.IsNotNull(mock.Object.GetList());Assert.IsNull(awaitmock.Object.GetListAsync());}[TestMethod]publicasyncTaskMakingItAllWork_IncludingAsync(){varmock=newMock<IFoo>();mock.SetReturnsDefault<ICollection<int>>([]);mock.SetReturnsDefault<IList<int>>([]);mock.SetReturnsDefault<List<int>>([]);mock.SetReturnsDefault(Task.FromResult(newList<int>()));Assert.IsNotNull(mock.Object.GetICollection());Assert.IsNotNull(mock.Object.GetIList());Assert.IsNotNull(mock.Object.GetList());Assert.IsNotNull(awaitmock.Object.GetListAsync());}[TestMethod]publicvoidBTW_WatchOutForReset_ItResetsTheDefaultSettingsAsWell(){varmock=newMock<IFoo>();mock.SetReturnsDefault<List<int>>([]);Assert.IsNotNull(mock.Object.GetList());mock.Reset();Assert.IsNull(mock.Object.GetList());mock.SetReturnsDefault<List<int>>([]);Assert.IsNotNull(mock.Object.GetList());}}
Version Info
Moq v4.20.70
The text was updated successfully, but these errors were encountered:
I think SetReturnsDefault<T> satisfies specific needs already, without introducing the potential for severe breaking changes if the current behavior were modified. I don't think that will happen.
Describe the Bug
Firstly,
DefaultValue.Empty
is described as: "Default behavior, which generates ... empty array and enumerables ...."It may be a matter of semantics but I expected something like a
List<T>
to be also treated as "enumerables" in this context. Depending on point of view, applying this to everything implementingIEnumerable<T>
may be too much (should astring
be "empty" or "null" in this mode?) but how about everything related toICollection<T>
?If nothing else, I hope this post will help others who encounter the same issue and wonder how to "make it work" (without explicit
Setup
for each such method on a mock).Secondly,
SetReturnsDefault
- it's currently very "picky" ;)It would be nice, IMHO, if it automatically supported all derived types... and/or wrapped in a Task, ValueTask.
Steps to Reproduce
This (MSTest-style) test code should explain it all:
Version Info
Moq v4.20.70
The text was updated successfully, but these errors were encountered: