question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AutoMoq a concrete dependency

See original GitHub issue

Hi,

I am trying to use a Moq of a concrete dependency.

[Theory, AutoMoqData]
public async Task Can_register_a_single_service_successfully(
    [Frozen] Mock<IdentityServerOptions> identityServerOptions,
    ServiceDiscoveryUsingIdentityServer sut
)
{...}

This throws an error where AutoFixture is trying to create a real instance of IdentityServerOptions to inject into my sut instead of using the frozen mock. If I remove the sut and manually create passing in the identityServerOptions.Object then it all works, so my working theory is that autofixture/automoq is not picking up that the frozen moq can satisfy the dependency and so tries to create a new instance.

I also tried Frozen(Matching.DirectBaseType) as the proxy generated by moq inherits from the requested value, but still no luck.

Is there any way around this as I can’t change IdentityServerOptions as it’s 3rd party and have more variables to inject to my sut, which makes the workaround a bit messy when I only need to access the one from my example.

Thanks Euan

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
Euan-McViecommented, Dec 8, 2020

Finally had a chance to test it and it works great. I made a minor improvement (I know its still not robust enough for all uses) so that you can still request the concrete object instead of the Mock wrapper.

public void Customize(IFixture fixture)
{
    Type? mockedType = (typeof(IMock<object>).IsAssignableFrom(ParameterInfo.ParameterType))
        ? ParameterInfo.ParameterType.GetGenericArguments()[0]
        : ParameterInfo.ParameterType;
    fixture.Customizations.Add(new MockRelay(new ExactTypeSpecification(mockedType)));
}

Allows

[Theory]
[AutoData]
public void DependencyShouldBeSameAsMockedObject(
    [Frozen][Mock] DependencyClass dependencyMock,
    SystemUnderTest sut)
{
    Assert.Same(dependencyMock, sut.Dependency);
}
1reaction
aivascucommented, Dec 7, 2020

@Kralizek yes it might be useful to have it in the AutoMoq lib. I’ll add an issue to the backlog, with a proposal for this feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AutoFixture with AutoMoq and concrete object injection
This doesn't work because when I call fixture.Create<IdentityApplicationService>() then in it's constructor a concrete TenantProvisioningService ...
Read more >
Register a concrete type in the auto-mocking container
By default, SpecsFor's auto-mocking container will create mock objects for your class's dependencies. However, you can easily change this behavior to inject ...
Read more >
Moq — Autofac 7.0.0 documentation
The Moq integration package allows you to automatically create mock dependencies for both concrete and mock abstract instances in unit tests using an...
Read more >
Integration with Moq
ConfigureMembers. By default, AutoMoq only provides values for non-concrete types. This can be overridden by setting the property ConfigureMembers to true .
Read more >
Adopt AutoMock with Autofac and Moq - drunk on monads
Mocking service dependencies manually in tests creates fragile tests that will need updating each time those dependencies change. Ideally, tests ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found