Exemple #1
0
        /// <summary>
        /// Asserts that an object has raised the <see cref="INotifyPropertyChanged.PropertyChanged"/> event for a particular property.
        /// </summary>
        /// <param name="eventSource">The object exposing the event.</param>
        /// <param name="propertyExpression">
        /// A lambda expression referring to the property for which the property changed event should have been raised, or
        /// <c>null</c> to refer to all properties.
        /// </param>
        /// <param name="reason">
        /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
        /// start with the word <i>because</i>, it is prepended to the message.
        /// </param>
        /// <param name="reasonArgs">
        /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])"/> compatible placeholders.
        /// </param>
        /// <remarks>
        /// You must call <see cref="MonitorEvents"/> on the same object prior to this call so that Fluent Assertions can
        /// subscribe for the events of the object.
        /// </remarks>
        public static IEventRecorder ShouldRaisePropertyChangeFor <T>(
            this T eventSource, Expression <Func <T, object> > propertyExpression,
            string reason, params object[] reasonArgs)
        {
            EventRecorder eventRecorder = eventSource.GetRecorderForEvent(PropertyChangedEventName);
            string        propertyName  = (propertyExpression != null) ? propertyExpression.GetPropertyInfo().Name : null;

            if (!eventRecorder.Any())
            {
                Execute.Assertion
                .BecauseOf(reason, reasonArgs)
                .FailWith("Expected object {0} to raise event {1} for property {2}{reason}, but it did not.",
                          eventSource, PropertyChangedEventName, propertyName);
            }

            return(eventRecorder.WithArgs <PropertyChangedEventArgs>(args => args.PropertyName == propertyName));
        }