Ejemplo n.º 1
0
        public async Task PublishSmsRequestFailedEvent_When_SmsCannotBeSent()
        {
            var eventList = new List <ISendSmsRequestFailed>();
            var bus       = Substitute.For <IBus>();

            bus.Publish(Arg.Any <ISendSmsRequestFailed>())
            .Returns(Task.FromResult((object)null))
            .AndDoes(x => eventList.Add(x.Arg <ISendSmsRequestFailed>()));
            var smsRepository = Substitute.For <ISmsRepository>();
            var logger        = Substitute.For <ILogger <SendSmsRequestReceivedConsumer> >();
            var smsMessages   = new List <ISmsMessage>
            {
                new SmsMessage("TEST", "TEST", "42342"),
                new SmsMessage("TEST", "TEST", "42342"),
                new SmsMessage("TEST", "TEST", "42342"),
            };
            var consumerContext = Substitute.For <ConsumeContext <ISendSmsRequestReceived> >();

            consumerContext.Message.SmsMessages.Returns(smsMessages);

            var consumer = new SendSmsRequestReceivedConsumer(
                bus,
                CreateSmsClient("text/plain", "HATA|310|Uzun mesaj metni"),
                smsRepository,
                logger,
                Substitute.For <ISmsLogRepository>());

            await consumer.Consume(consumerContext);

            eventList.First().UserFriendlyMessage.Should().Contain("Uzun mesaj metni");
            eventList.Should().HaveCount(smsMessages.Count);
        }
        public async Task DeductSmsCreditsFromAccountsCreditBalance_When_SmsSuccessfullySent()
        {
            const int    userId      = 15;
            const string messageBody = "Hello There!";
            var          smsDeducted = false;

            _webApiClient.DeductSmsCreditsAsync(userId, messageBody)
            .Returns(Task.FromResult((object)null))
            .AndDoes(x => smsDeducted = true);

            var publishEndPoint = Substitute.For <IPublishEndpoint>();
            var smsRepository   = Substitute.For <ISmsRepository>();
            var logger          = Substitute.For <ILogger <SendSmsRequestReceivedConsumer> >();
            var smsMessages     = new List <ISmsMessage>
            {
                new SmsMessage("TEST", messageBody, "42342"),
            };

            var consumerContext = Substitute.For <ConsumeContext <ISendSmsRequestReceived> >();

            consumerContext.Message.UserId.Returns(userId);
            consumerContext.Message.SmsMessages.Returns(smsMessages);

            var consumer = new SendSmsRequestReceivedConsumer(
                _webApiClient,
                publishEndPoint,
                CreateSmsClient("text/plain", "123123"),
                smsRepository,
                logger);

            await consumer.Consume(consumerContext);

            smsDeducted.Should().BeTrue();
        }