NSubstitute vs PRISM EventAggregator: Assert that calling a method triggers event with correct payload

前端 未结 3 1024
-上瘾入骨i
-上瘾入骨i 2020-12-21 09:39

Consider the below method that updates a person and publishes an event through the PRISM EventAggregator to indicate that the person has been updated.

I would like t

相关标签:
3条回答
  • 2020-12-21 09:55

    Just add a subscriber to your unit test and check the result of the delegate. See Prism's IEventAggregator unit tests as an example

    https://github.com/PrismLibrary/Prism/blob/master/Source/Prism.Tests/Events/PubSubEventFixture.cs#L14

    0 讨论(0)
  • 2020-12-21 10:08

    I'm not an experienced unit tester so I'm not sure this is the correct unit test to write. But anyway, this is how I tested it so far and it seems to do what I want.

    [Test]
    public void TestPersonUpdateSendsEventWithCorrectPayload()
    {
        // ARRANGE
        PersonUpdatedEventArgs payload = null;
        _eventAggregator
            .GetEvent<PersonUpdatedEvent>()
            .Subscribe(message => payload = message);
    
        // ACT
        _personService.UpdatePerson(5);
    
        // ASSERT
        payload.Should().NotBeNull();
        payload.Id.Should().Be(5);
    }
    

    Feedback welcome.

    0 讨论(0)
  • 2020-12-21 10:15

    You can actually substitute away the event aggregator like this:

    public class ToBeTested
    {
        private readonly IEventAggregator _eventAggregator;
    
        public ToBeTested( IEventAggregator eventAggregator )
        {
            _eventAggregator = eventAggregator;
        }
    
        public void DoStuff()
        {
            _eventAggregator.GetEvent<OneEvent>().Publish( "test" );
        }
    }
    
    public class OneEvent : PubSubEvent<string>
    {
    }
    
    [TestFixture]
    internal class Test
    {
        [Test]
        public void DoStuffTest()
        {
            var myEvent = new MyOneEvent();
            var myEventAggregator = Substitute.For<IEventAggregator>();
            myEventAggregator.GetEvent<OneEvent>().Returns( myEvent );
    
            var toBeTested = new ToBeTested( myEventAggregator );
            toBeTested.DoStuff();
    
            Assert.That( myEvent.ReceivedPayload, Is.EqualTo( "test" ) );
        }
    
        private class MyOneEvent : OneEvent
        {
            public override void Publish( string payload )
            {
                ReceivedPayload = payload;
            }
    
            public string ReceivedPayload
            {
                get;
                private set;
            }
        }
    }
    

    Note the fake event class that's foisted on the subject under test to get access to the payload.

    0 讨论(0)
提交回复
热议问题