private void AddOrReplaceWhenContinuation(LambdaExpression memberSelector, WhenContinuation continuation) { lock (this.continuationsSync) { var whenContinuationCollection = this.EnsureWhenContinuationCollection(memberSelector); // first remove any existing filtered continuation that matches the one we're trying to add to avoid memory leaks whenContinuationCollection.Remove(continuation); whenContinuationCollection.Add(continuation); } }
/// <summary> /// Begins the specification of what the mock should do when a given member is accessed. /// </summary> /// <param name="selector"> /// An expression that resolves to the member. /// </param> /// <returns> /// A continuation object with which the actions to be performed can be configured. /// </returns> public WhenContinuation <TMock> When(Expression <Action <TMock> > selector) { if (selector == null) { throw new ArgumentNullException("selector"); } var continuation = new WhenContinuation <TMock>(selector, ArgumentFiltersVisitor.FindArgumentFiltersWithin(selector.Body) ?? emptyArgumentFilters); this.AddOrReplaceWhenContinuation(selector, continuation); return(continuation); }
/// <summary> /// Begins the specification of what the mock should do when a given property is set. /// </summary> /// <typeparam name="TMember"> /// The type of the property. /// </typeparam> /// <param name="propertySelector"> /// An expression that resolves the property. /// </param> /// <param name="valueFilterSelector"> /// An optional expression that can provide filtering against the property value being set. /// </param> /// <returns> /// A continuation object with which the actions to be performed can be configured. /// </returns> public WhenContinuation <TMock, TMember> WhenPropertySet <TMember>(Expression <Func <TMock, TMember> > propertySelector, Expression <Func <TMember> > valueFilterSelector = null) { if (propertySelector == null) { throw new ArgumentNullException("propertySelector"); } if (valueFilterSelector == null) { valueFilterSelector = () => It.IsAny <TMember>(); } var filters = new ArgumentFilterCollection(); filters.Add(ArgumentFilterVisitor.FindArgumentFilterWithin(valueFilterSelector.Body)); var continuation = new WhenContinuation <TMock, TMember>(propertySelector, filters); this.AddOrReplaceWhenContinuation(propertySelector, continuation); return(continuation); }