示例#1
0
        public void When_asserting_that_an_event_was_not_raised_and_it_doesnt_exist_it_should_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();

            // Act
            Action act = () => monitor.Should().NotRaise("NonExistingEvent");

            // Assert
            act.Should().Throw <InvalidOperationException>().WithMessage(
                "Not monitoring any events named \"NonExistingEvent\".");
        }
示例#2
0
        public void When_the_event_sender_is_the_expected_object_it_should_not_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseEventWithSender();

            // Act
            Action act = () => monitor.Should().Raise("PropertyChanged").WithSender(subject);

            // Assert
            act.Should().NotThrow();
        }
示例#3
0
        public void When_a_monitored_class_event_has_fired_it_should_be_possible_to_reset_the_event_monitor()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var eventMonitor = subject.Monitor();
            subject.RaiseEventWithSenderAndPropertyName("SomeProperty");

            // Act
            eventMonitor.Clear();

            // Assert
            eventMonitor.Should().NotRaise("PropertyChanged");
        }
        public void When_a_property_changed_event_for_another_than_the_unexpected_property_was_raised_it_should_not_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseEventWithSenderAndPropertyName("SomeOtherProperty");

            // Act
            Action act = () => monitor.Should().NotRaisePropertyChangeFor(x => x.SomeProperty);

            // Assert
            act.Should().NotThrow();
        }
        public void When_an_expected_property_changed_event_was_raised_for_all_properties_it_should_not_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseEventWithSenderAndPropertyName(null);

            // Act
            Action act = () => monitor.Should().RaisePropertyChangeFor(null);

            // Assert
            act.Should().NotThrow();
        }
示例#6
0
        public void When_asserting_an_event_that_doesnt_exist_it_should_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitoredSubject = subject.Monitor();

            // Act
            // ReSharper disable once AccessToDisposedClosure
            Action act = () => monitoredSubject.Should().Raise("NonExistingEvent");

            // Assert
            act.Should().Throw <InvalidOperationException>().WithMessage(
                "Not monitoring any events named \"NonExistingEvent\".");
        }
示例#7
0
        public void When_a_property_changed_event_for_a_specific_property_was_not_raised_it_should_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();

            // Act
            Action act = () => monitor.Should().RaisePropertyChangeFor(x => x.SomeProperty, "the property was changed");

            // Assert
            act.Should().Throw <XunitException>().WithMessage(
                "Expected object " + Formatter.ToString(subject) +
                " to raise event \"PropertyChanged\" for property \"SomeProperty\" because the property was changed, but it did not*");
        }
示例#8
0
        public void TestUnsubscription_Simple()
        {
            EventRaisingClass observed = new EventRaisingClass();

            _eventRaised = false;
            var handler = new WeakEventHandler <EventArgs>(GetCallback(), h => observed.Event -= h);

            observed.Event += handler.Handler;
            Assert.AreEqual(1, observed.SubscriptionCount);

            // unregistration should detach event
            observed.Event -= handler.Handler;
            Assert.AreEqual(0, observed.SubscriptionCount);
            Assert.IsFalse(_eventRaised);
        }
示例#9
0
        public void When_an_event_was_not_raised_it_should_throw_and_use_the_reason()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();

            // Act
            Action act = () => monitor.Should().Raise("PropertyChanged", "{0} should cause the event to get raised", "Foo()");

            // Assert
            act.Should().Throw <XunitException>().WithMessage(
                "Expected object " + Formatter.ToString(subject) +
                " to raise event \"PropertyChanged\" because Foo() should cause the event to get raised, but it did not.");
        }
示例#10
0
        public void When_monitoring_an_object_with_invalid_property_expression_it_should_throw()
        {
            // Arrange
            var eventSource = new EventRaisingClass();

            using var monitor = eventSource.Monitor();
            Func <EventRaisingClass, int> func = e => e.SomeOtherProperty;

            // Act
            Action act = () => monitor.Should().RaisePropertyChangeFor(e => func(e));

            // Assert
            act.Should().Throw <ArgumentException>()
            .WithParameterName("expression");
        }
        public void When_nesting_monitoring_requests_scopes_should_be_isolated()
        {
            // Arrange
            var eventSource = new EventRaisingClass();

            using (var outerScope = eventSource.Monitor())
            {
                // Act
                using (var innerScope = eventSource.Monitor())
                {
                    // Assert
                    ((object)innerScope).Should().NotBeSameAs(outerScope);
                }
            }
        }
示例#12
0
        public void When_the_event_sender_is_not_the_expected_object_it_should_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseEventWithoutSender();

            // Act
            Action act = () => monitor.Should().Raise("PropertyChanged").WithSender(subject);

            // Assert
            act.Should().Throw <XunitException>()
            .WithMessage($"Expected sender {Formatter.ToString(subject)}, but found {{<null>}}.");
        }
示例#13
0
        public void When_a_property_agnostic_property_changed_event_for_was_not_raised_it_should_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();

            // Act
            Action act = () => monitor.Should().RaisePropertyChangeFor(null);

            // Assert
            act.Should().Throw <XunitException>().WithMessage(
                "Expected object " + Formatter.ToString(subject) +
                " to raise event \"PropertyChanged\" for property <null>, but it did not*");
        }
示例#14
0
        public void TestSubscription_Simple()
        {
            // subscription should not occur until handler is registered
            EventRaisingClass observed = new EventRaisingClass();

            Assert.AreEqual(0, observed.SubscriptionCount);

            _eventRaised    = false;
            observed.Event += new WeakEventHandler <EventArgs>(GetCallback()).Handler;

            // subscription should have occurred
            Assert.AreEqual(1, observed.SubscriptionCount);

            observed.RaiseEvent(EventArgs.Empty);
            Assert.IsTrue(_eventRaised);
        }
示例#15
0
        public void When_a_predicate_based_parameter_assertion_expects_more_parameters_then_an_event_has_it_should_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseNonConventionalEvent("first argument", 2, "third argument");

            // Act
            Action act = () => monitor
                         .Should().Raise(nameof(EventRaisingClass.NonConventionalEvent))
                         .WithArgs <string>(null, null, null, args => args == "fourth argument");

            // Assert
            act.Should().Throw <ArgumentException>().WithMessage("*4 parameters*String*, but*2*");
        }
示例#16
0
        public void When_a_property_changed_event_for_an_unexpected_property_was_raised_it_should_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseEventWithSenderAndPropertyName("SomeProperty");

            // Act
            Action act = () => monitor.Should().NotRaisePropertyChangeFor(x => x.SomeProperty, "nothing happened");

            // Assert
            act.Should().Throw <XunitException>().WithMessage(
                "Did not expect object " + Formatter.ToString(subject) +
                " to raise the \"PropertyChanged\" event for property \"SomeProperty\" because nothing happened, but it did.");
        }
        public void When_a_property_changed_event_was_raised_by_monitored_class_it_should_be_possible_to_reset_the_event_monitor()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using (var eventMonitor = subject.Monitor())
            {
                subject.RaiseEventWithSenderAndPropertyName("SomeProperty");

                // Act
                eventMonitor.Clear();

                // Assert
                eventMonitor.Should().NotRaisePropertyChangeFor(e => e.SomeProperty);
            }
        }
示例#18
0
        public void When_a_non_conventional_event_with_many_specific_arguments_was_raised_it_should_not_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseNonConventionalEvent("first argument", 2, "third argument");

            // Act
            Action act = () => monitor
                         .Should().Raise("NonConventionalEvent")
                         .WithArgs <string>(null, args => args == "third argument");

            // Assert
            act.Should().NotThrow();
        }
        public void When_the_expected_event_was_raised_it_should_not_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using (var monitor = subject.Monitor())
            {
                subject.RaiseEventWithoutSender();

                // Act
                Action act = () => monitor.Should().Raise("PropertyChanged");

                // Assert
                act.Should().NotThrow();
            }
        }
示例#20
0
        public void When_the_event_parameters_do_match_it_should_not_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseEventWithSenderAndPropertyName("SomeProperty");

            // Act
            Action act = () => monitor
                         .Should().Raise("PropertyChanged")
                         .WithArgs <PropertyChangedEventArgs>(args => args.PropertyName == "SomeProperty");

            // Assert
            act.Should().NotThrow();
        }
        public void When_monitoring_a_null_object_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            EventRaisingClass subject = null;

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow <NullReferenceException>().WithMessage("Cannot monitor the events of a <null> object.");
        }
示例#22
0
        public void When_injecting_a_null_predicate_into_WithArgs_it_should_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseNonConventionalEvent("first argument", 2, "third argument");

            // Act
            Action act = () => monitor.Should()
                         .Raise("NonConventionalEvent")
                         .WithArgs <string>(predicate: null);

            // Assert
            act.Should().ThrowExactly <ArgumentNullException>()
            .WithParameterName("predicate");
        }
示例#23
0
        public void When_the_property_changed_event_was_raised_for_the_wrong_property_it_should_throw_and_include_the_actual_properties_raised()
        {
            // Arrange
            var bar = new EventRaisingClass();

            using var monitor = bar.Monitor();
            bar.RaiseEventWithSenderAndPropertyName("OtherProperty1");
            bar.RaiseEventWithSenderAndPropertyName("OtherProperty2");
            bar.RaiseEventWithSenderAndPropertyName("OtherProperty2");

            // Act
            Action act = () => monitor.Should().RaisePropertyChangeFor(b => b.SomeProperty);

            // Assert
            act.Should().Throw <XunitException>()
            .WithMessage("Expected*property*SomeProperty*but*OtherProperty1*OtherProperty2*");
        }
示例#24
0
        public void When_a_non_conventional_event_with_a_specific_argument_was_not_raised_it_should_throw()
        {
            // Arrange
            const int wrongArgument = 3;
            var       subject       = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseNonConventionalEvent("first argument", 2, "third argument");

            // Act
            Action act = () => monitor
                         .Should().Raise("NonConventionalEvent")
                         .WithArgs <int>(args => args == wrongArgument);

            // Assert
            act.Should().Throw <XunitException>().WithMessage(
                "Expected at least one event with arguments matching (args == " + wrongArgument + "), but found none.");
        }
示例#25
0
        public void When_the_event_args_are_of_a_different_type_it_should_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseEventWithSenderAndPropertyName("SomeProperty");

            // Act
            Action act = () => monitor
                         .Should().Raise("PropertyChanged")
                         .WithArgs <CancelEventArgs>(args => args.Cancel);

            // Assert
            act
            .Should().Throw <ArgumentException>()
            .WithMessage("No argument of event PropertyChanged is of type *CancelEventArgs>*");
        }
        public void When_a_class_is_not_being_monitored_it_should_not_be_possible_to_get_a_recorder()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var eventSource = new EventRaisingClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action = () => eventSource.GetRecorderForEvent("PropertyChanged");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action.ShouldThrow <InvalidOperationException>()
            .WithMessage("*not being monitored*");
        }
示例#27
0
        public void When_a_specific_sender_is_expected_it_should_return_only_relevant_events()
        {
            // Arrange
            var observable = new EventRaisingClass();

            using var monitor = observable.Monitor();

            // Act
            observable.RaiseEventWithSpecificSender(observable);
            observable.RaiseEventWithSpecificSender(new object());

            // Assert
            var recording = monitor
                            .Should()
                            .Raise(nameof(observable.PropertyChanged))
                            .WithSender(observable);

            recording.Should().ContainSingle().Which.Parameters.First().Should().BeSameAs(observable);
        }
示例#28
0
        public void When_the_event_parameters_dont_match_it_should_throw()
        {
            // Arrange
            var subject = new EventRaisingClass();

            using var monitor = subject.Monitor();
            subject.RaiseEventWithoutSender();

            // Act
            Action act = () => monitor
                         .Should().Raise("PropertyChanged")
                         .WithArgs <PropertyChangedEventArgs>(args => args.PropertyName == "SomeProperty");

            // Assert
            act
            .Should().Throw <XunitException>()
            .WithMessage(
                "Expected at least one event with arguments matching (args.PropertyName == \"SomeProperty\"), but found none.");
        }
        public void When_asserting_an_event_that_doesnt_exist_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldRaise("NonExistingEvent");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<InvalidOperationException>().WithMessage(
                "Type <" + subject.GetType().Name + "> does not expose an event named \"NonExistingEvent\".");
        }
        public void When_asserting_that_an_event_was_not_raised_and_the_object_is_not_monitored_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldNotRaise("PropertyChanged");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow <InvalidOperationException>().WithMessage(
                "Object <FluentAssertions.Specs.EventAssertionSpecs+EventRaisingClass> is not being monitored for events or has already been garbage collected. " +
                "Use the MonitorEvents() extension method to start monitoring events.");
        }
        public void When_an_unexpected_event_was_not_raised_it_should_not_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();

            subject.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldNotRaise("PropertyChanged");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldNotThrow();
        }
        public void When_asserting_that_an_event_was_not_raised_and_the_object_is_not_monitored_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldNotRaise("PropertyChanged");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<InvalidOperationException>().WithMessage(
                "Object <FluentAssertions.Specs.EventAssertionSpecs+EventRaisingClass> is not being monitored for events or has already been garbage collected. " +
                    "Use the MonitorEvents() extension method to start monitoring events.");
        }
        public void When_a_property_changed_event_was_raised_for_the_right_property_it_should_not_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();
            subject.RaiseEventWithSenderAndPropertyName("SomeProperty");
            subject.RaiseEventWithSenderAndPropertyName("SomeOtherProperty");

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldRaisePropertyChangeFor(x => x.SomeProperty);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldNotThrow();
        }
        public void When_a_property_changed_event_for_a_specific_property_was_not_raised_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldRaisePropertyChangeFor(x => x.SomeProperty, "the property was changed");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<AssertFailedException>()
                .WithMessage(
                "Expected object*EventRaisingClass*to raise event*PropertyChanged*for*SomeProperty*because the property was changed, but it did not.");
        }
        public void When_a_property_changed_event_for_a_specific_property_was_raised_but_without_sender_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();
            subject.RaiseEventWithoutSender();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject
                .ShouldRaisePropertyChangeFor(x => x.SomeProperty, "the property was changed")
                .WithSender(subject);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<AssertFailedException>()
                .WithMessage("Expected sender*EventRaisingClass*, but found <null>.", ComparisonMode.Wildcard);
        }
        public void When_the_event_parameter_is_of_a_different_type_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();
            subject.RaiseEventWithSenderAndPropertyName("SomeProperty");

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject
                .ShouldRaise("PropertyChanged")
                .WithArgs<UnhandledExceptionEventArgs>(args => args.IsTerminating);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act
                .ShouldThrow<ArgumentException>()
                .WithMessage("No argument of event PropertyChanged is of type <System.UnhandledExceptionEventArgs>.");
        }
        public void When_the_event_parameters_do_match_it_should_not_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();
            subject.RaiseEventWithSenderAndPropertyName("SomeProperty");

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject
                .ShouldRaise("PropertyChanged")
                .WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == "SomeProperty");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldNotThrow();
        }
        public void When_a_property_changed_event_was_raised_by_monitored_class_it_should_be_possible_to_reset_the_event_monitor()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            var eventMonitor = subject.MonitorEvents();
            subject.RaiseEventWithSenderAndPropertyName("SomeProperty");

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            eventMonitor.Reset();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            subject.ShouldNotRaisePropertyChangeFor(e => e.SomeProperty);
        }
        public void When_the_event_parameters_dont_match_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();
            subject.RaiseEventWithoutSender();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject
                .ShouldRaise("PropertyChanged")
                .WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == "SomeProperty");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act
                .ShouldThrow<AssertFailedException>()
                .WithMessage(
                    "Expected at least one event with arguments matching (args.PropertyName == \"SomeProperty\"), but found none.");
        }
        public void When_a_class_is_not_being_monitored_it_should_not_be_possible_to_get_a_recorder()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var eventSource = new EventRaisingClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action = () => eventSource.GetRecorderForEvent("PropertyChanged");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action.ShouldThrow<InvalidOperationException>()
                .WithMessage("*not being monitored*");
        }
        public void When_a_monitored_class_in_not_referenced_anymore_it_should_be_garbage_collected_also_if_an_Event_passing_Sender_was_raised()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            var referenceToSubject = new WeakReference(subject);
            subject.MonitorEvents();
            subject.RaiseEventWithSender();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            subject = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            referenceToSubject.IsAlive.Should().BeFalse();
        }
        public void When_trying_to_attach_to_notify_property_changed_should_not_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            var eventMonitor = subject.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => eventMonitor.Attach(typeof(INotifyPropertyChanged));

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldNotThrow();
        }
        public void When_a_property_changed_event_for_an_unexpected_property_was_raised_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();
            subject.RaiseEventWithSenderAndPropertyName("SomeProperty");

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldNotRaisePropertyChangeFor(x => x.SomeProperty, "nothing happened");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<AssertFailedException>().WithMessage(
                "Did not expect object " + Formatter.ToString(subject) +
                    " to raise the \"PropertyChanged\" event for property \"SomeProperty\" because nothing happened, but it did.");
        }
        public void When_running_in_parallel_it_should_not_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            Action<int> action = _ =>
            {
                EventRaisingClass subject = new EventRaisingClass();
                subject.MonitorEvents();
                subject.RaiseEventWithSender();
                subject.ShouldRaise("PropertyChanged");
            };

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => Enumerable.Range(0, 1000)
                .AsParallel()
                .ForAll(action);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldNotThrow();
        }
        public void When_no_recorder_exists_for_an_event_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var eventSource = new EventRaisingClass();
            var eventMonitor = eventSource.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action = () => eventMonitor.GetEventRecorder("SomeEvent");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action.ShouldThrow<InvalidOperationException>()
                .WithMessage("Not monitoring any events named \"SomeEvent\".");
        }
        public void When_monitoring_class_requesting_to_monitor_again_should_return_same_monitor()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var eventSource = new EventRaisingClass();
            var eventMonitor = eventSource.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var newMonitor = eventSource.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            newMonitor.Should().BeSameAs(eventMonitor);
        }
        public void When_an_expected_property_changed_event_was_raised_for_all_properties_it_should_not_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();
            subject.RaiseEventWithSenderAndPropertyName(null);

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldRaisePropertyChangeFor(null);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldNotThrow();
        }
        public void When_asserting_that_an_event_was_not_raised_and_it_doesnt_exist_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldNotRaise("NonExistingEvent");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<InvalidOperationException>().WithMessage(
                "Not monitoring any events named \"NonExistingEvent\".");
        }
        public void When_a_property_agnostic_property_changed_event_for_was_not_raised_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldRaisePropertyChangeFor(null);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<AssertFailedException>().WithMessage(
                "Expected object " + Formatter.ToString(subject) +
                    " to raise event \"PropertyChanged\" for property <null>, but it did not.");
        }
        public void When_an_unexpected_event_was_raised_it_should_throw_and_use_the_reason()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();
            subject.RaiseEventWithoutSender();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldNotRaise("PropertyChanged", "{0} should cause the event to get raised", "Foo()");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<AssertFailedException>()
                .WithMessage("Expected object " + Formatter.ToString(subject) +
                    " to not raise event \"PropertyChanged\" because Foo() should cause the event to get raised, but it did.");
        }
        public void When_monitoring_class_it_should_be_possible_to_obtain_a_recorder()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var eventSource = new EventRaisingClass();
            eventSource.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var recorder = eventSource.GetRecorderForEvent("PropertyChanged");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            recorder.Should().NotBeNull();
            recorder.EventObject.Should().BeSameAs(eventSource);
            recorder.EventName.Should().Be("PropertyChanged");
        }
        public void When_an_unexpected_event_was_not_raised_it_should_not_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldNotRaise("PropertyChanged");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldNotThrow();
        }
        public void When_no_recorder_exists_for_an_event_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var eventSource = new EventRaisingClass();
            eventSource.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action = () => eventSource.GetRecorderForEvent("SomeEvent");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action.ShouldThrow<InvalidOperationException>()
                .WithMessage("*not expose*SomeEvent*");
        }
        public void When_the_event_sender_is_not_the_expected_object_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();
            subject.RaiseEventWithoutSender();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldRaise("PropertyChanged").WithSender(subject);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<AssertFailedException>().WithMessage("Expected sender " + Formatter.ToString(subject) +
                ", but found <null>.");
        }
        public void When_the_event_sender_is_the_expected_object_it_should_not_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            subject.MonitorEvents();
            subject.RaiseEventWithSender();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => subject.ShouldRaise("PropertyChanged").WithSender(subject);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldNotThrow();
        }
        public void When_trying_to_attach_to_something_other_than_notify_property_changed_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //----------------------------------------------------------------------------------------------------------
            var subject = new EventRaisingClass();
            var eventMonitor = subject.MonitorEvents();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => eventMonitor.Attach(typeof(EventRaisingClass));

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<NotSupportedException>()
                .WithMessage("Cannot monitor events of type \"EventRaisingClass\".");
        }