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

AutoMock Create method cannot find the correct overloaded constructor #31

Closed
mciancia8 opened this issue Mar 15, 2020 · 1 comment · Fixed by #32
Closed

AutoMock Create method cannot find the correct overloaded constructor #31

mciancia8 opened this issue Mar 15, 2020 · 1 comment · Fixed by #32
Labels

Comments

@mciancia8
Copy link

mciancia8 commented Mar 15, 2020

I recently upgraded from 4.3.0 to 5.0.0 and I had a bunch of tests that were passing in version 4.3.0 now fail for 5.0.0.

Here is an example of a class that I was testing, I have removed everything but the constructors:

public class TestClass
{
	public TestClass(int param1)
		: this(param1, TimeSpan.Zero)
	{
	}

	public TestClass(int param1, TimeSpan param2)
	{
	}
}

Here is my unit test:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void Test1()
    {
        using (AutoMock autoMock = AutoMock.GetStrict())
        {
            var testClass = autoMock.Create<TestClass>(new NamedParameter("param1", 10));
        }
    }
}

This is the exception that I get:

Test method UnitTestProject1.UnitTest1.Test1 threw exception: 
Autofac.Core.DependencyResolutionException: An exception was thrown while activating UnitTestProject1.TestClass -> System.TimeSpan -> System.Int32. ---> Autofac.Core.Activators.Reflection.NoConstructorsFoundException: No accessible constructors were found for the type 'System.Int32'.
    at Autofac.Core.Activators.Reflection.DefaultConstructorFinder.GetDefaultPublicConstructors(Type type) in C:\projects\autofac\src\Autofac\Core\Activators\Reflection\DefaultConstructorFinder.cs:line 81
   at Autofac.Core.Activators.Reflection.DefaultConstructorFinder.FindConstructors(Type targetType) in C:\projects\autofac\src\Autofac\Core\Activators\Reflection\DefaultConstructorFinder.cs:line 71
   at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) in C:\projects\autofac\src\Autofac\Core\Activators\Reflection\ReflectionActivator.cs:line 110
   at Autofac.Core.Resolving.InstanceLookup.CreateInstance(IEnumerable`1 parameters) in C:\projects\autofac\src\Autofac\Core\Resolving\InstanceLookup.cs:line 138
--- End of inner exception stack trace ---
    at Autofac.Core.Resolving.InstanceLookup.CreateInstance(IEnumerable`1 parameters) in C:\projects\autofac\src\Autofac\Core\Resolving\InstanceLookup.cs:line 150
   at Autofac.Core.Resolving.InstanceLookup.<Execute>b__8_0() in C:\projects\autofac\src\Autofac\Core\Resolving\InstanceLookup.cs:line 92
   at Autofac.Core.Lifetime.LifetimeScope.CreateSharedInstance(Guid id, Func`1 creator) in C:\projects\autofac\src\Autofac\Core\Lifetime\LifetimeScope.cs:line 301
   at Autofac.Core.Resolving.InstanceLookup.Execute() in C:\projects\autofac\src\Autofac\Core\Resolving\InstanceLookup.cs:line 91
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request) in C:\projects\autofac\src\Autofac\Core\Resolving\ResolveOperation.cs:line 125
   at Autofac.Core.Resolving.ResolveOperation.Execute(ResolveRequest request) in C:\projects\autofac\src\Autofac\Core\Resolving\ResolveOperation.cs:line 87
   at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(ResolveRequest request) in C:\projects\autofac\src\Autofac\Core\Lifetime\LifetimeScope.cs:line 279
   at Autofac.Core.Container.ResolveComponent(ResolveRequest request) in C:\projects\autofac\src\Autofac\Core\Container.cs:line 151
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) in C:\projects\autofac\src\Autofac\ResolutionExtensions.cs:line 1070
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters) in C:\projects\autofac\src\Autofac\ResolutionExtensions.cs:line 892
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters) in C:\projects\autofac\src\Autofac\ResolutionExtensions.cs:line 314
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, Parameter[] parameters) in C:\projects\autofac\src\Autofac\ResolutionExtensions.cs:line 331
   at Autofac.Extras.Moq.AutoMock.Create[T](Boolean isMock, Parameter[] parameters) in C:\projects\autofac-extras-moq\src\Autofac.Extras.Moq\AutoMock.cs:line 202
   at Autofac.Extras.Moq.AutoMock.Create[T](Parameter[] parameters) in C:\projects\autofac-extras-moq\src\Autofac.Extras.Moq\AutoMock.cs:line 174
   at UnitTestProject1.UnitTest1.Test1() in C:\Users\mcianciarulo\source\repos\UnitTestProject1\UnitTest1.cs:line 21
@alistairjevans
Copy link
Member

Thanks for raising this.

It appears as though the Moq Handler attempts to register the TimeSpan as a service, which is obviously not what you want. When you don't specify the TimeSpan parameter at resolve time, we try to register it, and things go wrong.

Some of the filtering applied inside the ACTNAR source we were using before hasn't carried over.

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.

2 participants