Пример #1
0
        public void should_consume_event_in_the_classic_way()
        {
            var sut = new SomeEventBasedClass();

            var       messages  = new List <string>();
            MyHandler myHandler = (sender, args) => messages.Add(args.SomeField);

            sut.Sent += myHandler;

            sut.Send("message 1");
            sut.Send("message 2");
            sut.Send("message 3");

            messages[0].Should().Be("message 1");
            messages[1].Should().Be("message 2");
            messages[2].Should().Be("message 3");

            sut.Sent -= myHandler;
        }
Пример #2
0
        public void should_consume_the_same_events_as_a_stream()
        {
            var sut    = new SomeEventBasedClass();
            var stream = Observable.FromEventPattern <MyHandler, MyHandlerArgs>(
                handler => sut.Sent += handler,
                handler => sut.Sent -= handler);

            var result = new List <string>();

            var stringObserver = new StringObserver <string>(result);

            using (var disposable = stream.Select(a => a.EventArgs.SomeField).Subscribe(stringObserver))
            {
                sut.Send("message 1");
                sut.Send("message 2");
                sut.Send("message 3");
            }

            result[0].Should().Be("OnNext(message 1)");
            result[1].Should().Be("OnNext(message 2)");
            result[2].Should().Be("OnNext(message 3)");
        }
Пример #3
0
        public void should_create_an_observable_from_EventPattern_using_the_short_form()
        {
            var sut = new SomeEventBasedClass();
//            var stream = Observable.FromEventPattern<MyHandler, MyHandlerArgs>(
//                handler => sut.Sent += handler,
//                handler => sut.Sent -= handler);

            var stream = Observable.FromEventPattern <MyHandlerArgs>(sut, nameof(SomeEventBasedClass.Sent));

            var result         = new List <string>();
            var stringObserver = new StringObserver <string>(result);

            using (var disposable = stream.Select(a => a.EventArgs.SomeField).Subscribe(stringObserver))
            {
                sut.Send("message 1");
                sut.Send("message 2");
                sut.Send("message 3");
            }

            result[0].Should().Be("OnNext(message 1)");
            result[1].Should().Be("OnNext(message 2)");
            result[2].Should().Be("OnNext(message 3)");
        }