/// <summary> /// Asserts that an object has not raised a particular event. /// </summary> /// <param name="eventSource">The object exposing the event.</param> /// <param name="eventName"> /// The name of the event that should not be raised. /// </param> /// <param name="because"> /// 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="becauseArgs"> /// 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 void ShouldNotRaise( this object eventSource, string eventName, string because, params object[] becauseArgs) { IEventRecorder eventRecorder = EventMonitor.Get(eventSource).GetEventRecorder(eventName); if (eventRecorder.Any()) { Execute.Assertion .BecauseOf(because, becauseArgs) .FailWith("Expected object {0} to not raise event {1}{reason}, but it did.", eventSource, eventName); } }
/// <summary> /// Asserts that an object has not 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. /// </param> /// <param name="because"> /// 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="becauseArgs"> /// 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="AssertionExtensions.MonitorEvents"/> on the same object prior to this call so that Fluent Assertions can /// subscribe for the events of the object. /// </remarks> public static void ShouldNotRaisePropertyChangeFor <T>( this T eventSource, Expression <Func <T, object> > propertyExpression, string because, params object[] becauseArgs) { IEventRecorder eventRecorder = EventMonitor.Get(eventSource).GetEventRecorder(PropertyChangedEventName); string propertyName = propertyExpression.GetPropertyInfo().Name; if (eventRecorder.Any(@event => GetAffectedPropertyName(@event) == propertyName)) { Execute.Assertion .BecauseOf(because, becauseArgs) .FailWith("Did not expect object {0} to raise the {1} event for property {2}{reason}, but it did.", eventSource, PropertyChangedEventName, propertyName); } }
/// <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="because"> /// 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="becauseArgs"> /// 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="AssertionExtensions.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 because, params object[] becauseArgs) { IEventRecorder eventRecorder = EventMonitor.Get(eventSource).GetEventRecorder(PropertyChangedEventName); string propertyName = (propertyExpression != null) ? propertyExpression.GetPropertyInfo().Name : null; if (!eventRecorder.Any()) { Execute.Assertion .BecauseOf(because, becauseArgs) .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)); }