Mock.Of<T>((m) => ...) doesn't play well with Microsoft IOptions<T>
repro repo: https://github.com/mariotee/Moq-MockOf-Issue
I have two identical unit tests. The difference is the version of Moq. UnitTestProject1 uses Moq 4.14.5 and fails to load IOptions into the properties. UnitTestProject2 uses Moq 4.13.1 and passes
UPDATE: can confirm this is also broken in 4.14.0. so it might have been somewhere within that minor version change
Thanks for reporting @mariotee, looks like a regression. I never stop being surprised by the simple use cases that our unit/regression test suite doesn't cover... we're still lacking some rather basic tests it seems.
Note to self, here's the repro in abbreviated form:
[Fact]
public void Test()
{
Mock.Of<IOptions<DatabaseOptions>>(
o => o.Value.ConnectionString == "connection" && o.Value.MaxConnections == 1);
}
public interface IOptions<out TOptions>
{
TOptions Value { get; }
}
public class DatabaseOptions
{
public string ConnectionString { get; set; }
public int MaxConnections { get; set; }
}
This is not specific to IOptions. It looks like using Mock.Of with any sub-properties results in System.Reflection.TargetException : Object does not match target type.
Example: var orderItem = Mock.Of<OrderItem>(x => x.Order.Id == 123);