public void Test_EmailSend_Success()
        {
            var subject = $"NUnit-Tests";
            var body    = $"This is a test from {nameof(Test_EmailSend_Success)}";

            Assert.DoesNotThrow(() => _sut.Send(subject, body));

            var warningCalls  = _logger.Invocations.Where(x => x.Method.Name == nameof(_logger.Object.Warn));
            var errorCalls    = _logger.Invocations.Where(x => x.Method.Name == nameof(_logger.Object.Error));
            var criticalCalls = _logger.Invocations.Where(x => x.Method.Name == nameof(_logger.Object.Critical));

            // ensure no catastrophes
            Assert.AreEqual(0, errorCalls.Count());
            Assert.AreEqual(0, criticalCalls.Count());

            if (_shouldSendEmail)
            {
                // assertions when *actually* sending a SMTP message
                Assert.IsTrue(warningCalls.Count() == 0, $"At least one call to {nameof(_logger.Object.Warn)} was made!");

                // nothing left to do, so exit early
                return;
            }

            // assertions when *faking* the send of a SMTP message
            Assert.IsTrue(warningCalls.Count() > 0, $"No calls to {nameof(_logger.Object.Warn)} were made!");
            Assert.IsTrue(warningCalls.Count() == 1, $"More than one call to {nameof(_logger.Object.Warn)} was made!");
            var warningMessage = warningCalls.First().Arguments.First();

            Assert.AreEqual("Did not send email because _isEmailSendingAllowed was False", warningMessage, "Warning message did not match!");
        }
        public void Test_EmailSend_MissingRecipientAddress_Fail(string address)
        {
            var rs = _defaultObjs.RuntimeSettingsMock;

            rs.Setup(x => x.EmailRecipientAddress).Returns(address);
            _sut = new SmtpHelper(rs.Object, _logger.Object, _shouldSendEmail);
            Assert.DoesNotThrow(() => _sut.Send("TestSubject", "TestBody"));
            var loggerErrors = _logger.Invocations.Where(x => x.Method.Name == nameof(_logger.Object.Error));

            Assert.AreEqual(1, loggerErrors.Count());
            var errorMessage = loggerErrors.First().Arguments.First();

            Assert.IsTrue(errorMessage.ToString().Contains("Unable to send email because setting 'EmailRecipientAddress'"), "Unable to find the target error message!");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This EXE is only used for ad-hoc testing of various functionality.  It is not to be used for actual builds or MSI files.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // serilog
            IRuntimeSettings rs     = RuntimeSettingsProvider.Instance.GetRuntimeSettings();
            IFileManager     fm     = new LocalFileManager();
            IAppLogger       logger = new SerilogAppLogger(rs, fm);

            ISmtpHelper smtp    = new SmtpHelper(rs, logger);
            var         subject = "Console";
            var         body    = DateTime.Now.TimeOfDay.ToString();

            smtp.Send(subject, body);

            Console.WriteLine();
            Console.WriteLine("Press ENTER to quit");
            Console.ReadLine();
        }