WithArgumentsForConstructor() extension method calls constructor
Asked Answered
G

1

7

According to the FakeItEasy tutorial here the WithArgumentsForConstructor() extension method does not call the class constructor:

// Specifying arguments for constructor using expression. This is refactoring friendly!
// The constructor seen here is never actually invoked. It is an expression and it's purpose
// is purely to communicate the constructor arguments which will be extracted from it
var foo = A.Fake<FooClass>(x => x.WithArgumentsForConstructor(() => new FooClass("foo", "bar")));

However, the breakpoint in my Person class constructor is triggered in my test below. How so? Am I using WithArgumentsForConstructor() incorrectly?

[Test]
public void Constructor_With_Arguments()
{
    var driver = A.Fake<Person>(x => x.WithArgumentsForConstructor(() => new Person("Jane", 42)));
    var age = driver.GetAge();
    Assert.AreEqual(42, age);
}

Stack trace:

MyStuff.Tests.Domain.dll!MyStuff.Tests.Domain.Driver.Person(string name, int age) Line 61   C#
DynamicProxyGenAssembly2!Castle.Proxies.DriverProxy.DriverProxy(Castle.DynamicProxy.IInterceptor[] value, string value, int value)  Unknown
[Native to Managed Transition]  
[Managed to Native Transition]  
mscorlib.dll!System.RuntimeType.CreateInstanceImpl(System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes, ref System.Threading.StackCrawlMark stackMark)  Unknown
mscorlib.dll!System.Activator.CreateInstance(System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes) Unknown
mscorlib.dll!System.Activator.CreateInstance(System.Type type, object[] args)   Unknown
FakeItEasy.dll!Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(System.Type proxyType, System.Collections.Generic.List<object> proxyArguments, System.Type classToProxy, object[] constructorArguments)  Unknown
FakeItEasy.dll!Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(System.Type classToProxy, System.Type[] additionalInterfacesToProxy, Castle.DynamicProxy.ProxyGenerationOptions options, object[] constructorArguments, Castle.DynamicProxy.IInterceptor[] interceptors) Unknown
FakeItEasy.dll!FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyGenerator.GenerateClassProxy(System.Type typeOfProxy, System.Collections.Generic.IEnumerable<object> argumentsForConstructor, Castle.DynamicProxy.IInterceptor interceptor, System.Collections.Generic.IEnumerable<System.Type> allInterfacesToImplement)   Unknown
FakeItEasy.dll!FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyGenerator.DoGenerateProxy(System.Type typeOfProxy, System.Collections.Generic.IEnumerable<System.Type> additionalInterfacesToImplement, System.Collections.Generic.IEnumerable<object> argumentsForConstructor, Castle.DynamicProxy.IInterceptor interceptor)   Unknown
FakeItEasy.dll!FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyGenerator.CreateProxyGeneratorResult(System.Type typeOfProxy, System.Collections.Generic.IEnumerable<System.Type> additionalInterfacesToImplement, System.Collections.Generic.IEnumerable<object> argumentsForConstructor, FakeItEasy.Core.IFakeCallProcessorProvider fakeCallProcessorProvider)    Unknown
FakeItEasy.dll!FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyGenerator.GenerateProxy(System.Type typeOfProxy, System.Collections.Generic.IEnumerable<System.Type> additionalInterfacesToImplement, System.Collections.Generic.IEnumerable<object> argumentsForConstructor, FakeItEasy.Core.IFakeCallProcessorProvider fakeCallProcessorProvider) Unknown
FakeItEasy.dll!FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyGenerator.GenerateProxy(System.Type typeOfProxy, System.Collections.Generic.IEnumerable<System.Type> additionalInterfacesToImplement, System.Collections.Generic.IEnumerable<object> argumentsForConstructor, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> customAttributeBuilders, FakeItEasy.Core.IFakeCallProcessorProvider fakeCallProcessorProvider)  Unknown
FakeItEasy.dll!FakeItEasy.Creation.ProxyGeneratorSelector.GenerateProxy(System.Type typeOfProxy, System.Collections.Generic.IEnumerable<System.Type> additionalInterfacesToImplement, System.Collections.Generic.IEnumerable<object> argumentsForConstructor, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> customAttributeBuilders, FakeItEasy.Core.IFakeCallProcessorProvider fakeCallProcessorProvider)  Unknown
FakeItEasy.dll!FakeItEasy.Creation.FakeObjectCreator.GenerateProxy(System.Type typeOfFake, FakeItEasy.Creation.FakeOptions fakeOptions, System.Collections.Generic.IEnumerable<object> argumentsForConstructor) Unknown
FakeItEasy.dll!FakeItEasy.Creation.FakeObjectCreator.CreateFake(System.Type typeOfFake, FakeItEasy.Creation.FakeOptions fakeOptions, FakeItEasy.Creation.IDummyValueCreationSession session, bool throwOnFailure)   Unknown
FakeItEasy.dll!FakeItEasy.Creation.DefaultFakeAndDummyManager.CreateFake(System.Type typeOfFake, FakeItEasy.Creation.FakeOptions options)   Unknown
FakeItEasy.dll!FakeItEasy.Creation.DefaultFakeCreatorFacade.CreateFake<SysSurge.DynMock.Tests.Domain.Driver>(System.Action<FakeItEasy.Creation.IFakeOptionsBuilder<SysSurge.DynMock.Tests.Domain.Driver>> options)  Unknown

...

Garbers answered 1/5, 2016 at 21:57 Comment(0)
B
7

You're using it right.

But you understand it wrong.

// The constructor seen here is never actually invoked...

It means that it won't call the constructor exactly in this place. It will parse this expression and use the parameters to invoke the constructor somewhere inside. But it will invoke the constructor.

You can check this to put the breakpoint right inside the expression.

Beriosova answered 1/5, 2016 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.