public override void Apply(IInterceptedFakeObjectCall fakeObjectCall) { Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall)); Fake.GetFakeManager(fakeObjectCall.FakedObject).MutateUserRules(allUserRules => { // Setting the property adds a PropertyBehaviorRule to store the assigned value. // The PropertyBehaviorRule is added at the end of user rules to avoid overriding // explicit configurations of the property made with CallTo. var existingRule = allUserRules .Select(metadata => metadata.Rule) .OfType <PropertyBehaviorRule>() .LastOrDefault(rule => rule.IsMatchForSetter(fakeObjectCall)); if (existingRule is not null) { existingRule.Value = fakeObjectCall.Arguments.Last(); } else { var newRule = CallRuleMetadata.CalledOnce( new PropertyBehaviorRule(fakeObjectCall.Method) { Indices = fakeObjectCall.Arguments.Take(fakeObjectCall.Arguments.Count - 1).ToArray(), Value = fakeObjectCall.Arguments.Last() }); allUserRules.AddLast(newRule); } }); }
public override void Apply(IInterceptedFakeObjectCall fakeObjectCall) { Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall)); var newRule = CallRuleMetadata.CalledOnce( new PropertyBehaviorRule(fakeObjectCall.Method) { Value = fakeObjectCall.GetDefaultReturnValue(), Indices = fakeObjectCall.Arguments.ToArray(), }); Fake.GetFakeManager(fakeObjectCall.FakedObject).MutateUserRules( allUserRules => allUserRules.AddFirst(newRule)); newRule.Rule.Apply(fakeObjectCall); }