Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock.Of<T>((m) => ...) doesn't play well with Microsoft IOptions<T> #1039

Closed
mariotee opened this issue Jul 13, 2020 · 2 comments · Fixed by #1061
Closed

Mock.Of<T>((m) => ...) doesn't play well with Microsoft IOptions<T> #1039

mariotee opened this issue Jul 13, 2020 · 2 comments · Fixed by #1061
Labels
Milestone

Comments

@mariotee
Copy link

mariotee commented Jul 13, 2020

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

@stakx stakx added the bug label Jul 15, 2020
@stakx
Copy link
Contributor

stakx commented Jul 15, 2020

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; }
}

@arnileibovits
Copy link

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants