Exception when using more than 2 mocks in a test function
See original GitHub issueWhen installing Autofixture.AutoMoc 4.8.0 with Moq 4.8.0 or above this exception happens:
If I leave 2 Mocks, it will work fine. The function I try:
[Theory, InlineAutoData]
public void test(
Mock<ILog> a,
Mock<ILog> b,
Mock<ILog> c)
{
}
Exception:…
Result StackTrace:
at AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\TerminatingWithPathSpecimenBuilder.cs:line 82
at AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\BehaviorRoot.cs:line 71
at AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\RecursionGuard.cs:line 167
at AutoFixture.Fixture.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Fixture.cs:line 276
at AutoFixture.Kernel.SpecimenContext.Resolve(Object request) in C:\projects\autofixture\Src\AutoFixture\Kernel\SpecimenContext.cs:line 33
at AutoFixture.Kernel.SeedIgnoringRelay.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\SeedIgnoringRelay.cs:line 42
at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
at AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\Postprocessor.cs:line 271
at AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\AutoPropertiesTarget.cs:line 71
at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
at AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\TerminatingWithPathSpecimenBuilder.cs:line 78
at AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\BehaviorRoot.cs:line 71
at AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\RecursionGuard.cs:line 167
at AutoFixture.Fixture.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Fixture.cs:line 276
at AutoFixture.Kernel.SpecimenContext.Resolve(Object request) in C:\projects\autofixture\Src\AutoFixture\Kernel\SpecimenContext.cs:line 33
at AutoFixture.Kernel.ParameterRequestRelay.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\ParameterRequestRelay.cs:line 32
at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
at AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\Postprocessor.cs:line 271
at AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\AutoPropertiesTarget.cs:line 71
at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
at AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\TerminatingWithPathSpecimenBuilder.cs:line 78
at AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\BehaviorRoot.cs:line 71
at AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\RecursionGuard.cs:line 167
at AutoFixture.Fixture.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Fixture.cs:line 276
at AutoFixture.Xunit2.AutoDataAttribute.GetData(MethodInfo testMethod) in C:\projects\autofixture\Src\AutoFixture.xUnit2\AutoDataAttribute.cs:line 116
at AutoFixture.Xunit2.CompositeDataAttribute.<>c__DisplayClass5_0.<GetData>b__0(DataAttribute attr) in C:\projects\autofixture\Src\AutoFixture.xUnit2\CompositeDataAttribute.cs:line 61
at System.Linq.Enumerable.<>c__DisplayClass7_0`3.<CombineSelectors>b__0(TSource x)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at AutoFixture.Xunit2.EnumerableExtensions.<Zip>d__0`2.MoveNext() in C:\projects\autofixture\Src\AutoFixture.xUnit2\EnumerableExtensions.cs:line 20
----- Inner Stack Trace -----
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at AutoFixture.Kernel.AutoPropertiesCommand`1.Execute(Object specimen, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\AutoPropertiesCommand.cs:line 256
at AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\Postprocessor.cs:line 282
at AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\AutoPropertiesTarget.cs:line 71
at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
at AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\TerminatingWithPathSpecimenBuilder.cs:line 58
----- Inner Stack Trace -----
at Moq.Mock.set_DefaultValue(DefaultValue value) in C:\projects\moq4\Source\Mock.cs:line 175
Result Message:
AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance from Moq.Mock`1[Common.Logging.ILog] because creation unexpectedly failed with exception. Please refer to the inner exception to investigate the root cause of the failure.
Request path:
Moq.Mock`1[Common.Logging.ILog] c
Moq.Mock`1[Common.Logging.ILog]
Inner exception messages:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: value
---- System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
-------- System.ArgumentOutOfRangeException : Specified argument was out of the range of valid values.
Parameter name: value
With Version 4.7.145 of Moq, it works ok for more than 3 mocks.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Exception when using more than two mocks in a test
The issue happens when AutoFixture tries to auto-assign properties to a newly created Mock object. The auto-generated DefaultValue property ...
Read more >C#: Test multiple Exception with xUnit and moq
In this week, I had an opportunity to write unit test code to test a method which returns different type of Exception based...
Read more >unittest.mock — getting started
A common use case is to mock out classes instantiated by your code under test. When you patch a class, then that class...
Read more >unittest.mock — mock object library
Mock is designed for use with unittest and is based on the 'action -> assertion' pattern instead of 'record -> replay' used by...
Read more >Intro to Python Mocks #2: Mocking Exceptions | Python tutorial
This video is the second part of Python testing using Mocks series (or just Python Mocks ). In this video we'll touch the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You should have your own
InlineAutoMoqData
, instead ofInlineAutoData
:Should work.
Hi @ronenfe,
AutoData
andInlineAutoData
do not become aware of Moq when you just install theAutoFixture.AutoMoq
package. Instead, you should create your own data attributes derived from the default ones, customizefixture
with Moq support and use them instead.Implementation might be like following:
It should help 😉