根据FakeItEasy教程here,WithArgumentsForConstructor()
扩展方法不会调用类构造函数:
// 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")));
但是,我的Person
类构造函数中的断点在下面的测试中被触发。怎么会这样?我错误地使用WithArgumentsForConstructor()
吗?
[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);
}
堆栈跟踪:
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
...
答案 0 :(得分:3)
您正确使用它。
但你理解错了。
// The constructor seen here is never actually invoked...
这意味着它不会在这个地方完全调用构造函数。它将解析此表达式并使用参数在内部的某处调用构造函数。但会调用构造函数。
您可以选中此项以将断点放在表达式中。