Ejemplo n.º 1
0
 public static void ReplaceObjectDefinition(this IConfigurableApplicationContext applicationContext, string name,
                                            IObjectDefinition definition)
 {
     if (definition?.ObjectType != typeof(SurrogateFactoryObject))
     {
         // Proceed here if the object definition will replace the object definition of the
         // SurrogateFactoryObject type.
         var factoryObject = applicationContext.GetFactoryObject(name);
         if (factoryObject is SurrogateFactoryObject && factoryObject.IsSingleton)
         {
             // If it happens that Sprint.NET decides to cache this singleton object, then
             // proceed to clear its state, like call counters. This is to make sure that
             // a new test starts with a clean mock/spy surrogate.
             var fakeObject = applicationContext.GetObject(name);
             FakeItEasy.Fake.ClearRecordedCalls(fakeObject);
         }
     }
     applicationContext.ObjectFactory.RemoveSingleton(name);
     applicationContext.RegisterObjectDefinition(name, definition);
 }
        protected override object GetObjectFromField(FieldInfo fieldInfo, object o)
        {
            var target = fieldInfo.GetValue(o);

            if (TypesToSpySubrogateSet.Contains(fieldInfo.FieldType))
            {
                var names = GetAppContextObjectNameListFromType(fieldInfo.FieldType);
                if (names.Any())
                {
                    // TODO: Deal with the case that there are several names. Using always the First
                    // may not be okay in all cases.
                    var spyObjectDef = _applicationContext.GetFactoryObject(names.First());
                    if (spyObjectDef is SurrogateFactoryObject)
                    {
                        target = (spyObjectDef as SurrogateFactoryObject).ObjectToFake;
                    }
                }
            }
            while (AopUtils.IsAopProxy(target))
            {
                target = ((IAdvised)target).TargetSource.GetTarget();
            }
            return(target);
        }