public void ThrowsExceptionWhenFailingToCountMessages()
            {
                // Arrange
                var manager = new SmsManager(_serialPort);

                _serialPort.When(sp => sp.Write(Arg.Is <string>(a => a == "AT\r"))).Do(c =>
                {
                    _serialPort.ReadExisting().Returns("\r\nOK\r\n");
                    manager._receiveNow.Set();
                });
                _serialPort.When(sp => sp.Write(Arg.Is <string>(a => a == "AT+CMGF=1\r"))).Do(c =>
                {
                    _serialPort.ReadExisting().Returns("\r\nOK\r\n");
                    manager._receiveNow.Set();
                });
                _serialPort.When(sp => sp.Write(Arg.Is <string>(a => a == "AT+CPMS?\r"))).Do(c =>
                {
                    _serialPort.ReadExisting().Returns("\r\nERROR\r\n");
                    manager._receiveNow.Set();
                });

                // Act
                // Assert
                Assert.That(() => manager.CountSMSmessages(_serialPort),
                            Throws.Exception.TypeOf <ApplicationException>()
                            .With.Message.EqualTo("No success message was received."));
            }
            public void SuccessfullyCountsMessages()
            {
                // READ THIS: http://tech.findmypast.com/dont-mock-what-you-dont-own/
                // Arrange
                var manager = new SmsManager(_serialPort);

                _serialPort.When(sp => sp.Write(Arg.Is <string>(a => a == "AT\r"))).Do(c =>
                {
                    _serialPort.ReadExisting().Returns("\r\nOK\r\n");
                    manager._receiveNow.Set();
                });
                _serialPort.When(sp => sp.Write(Arg.Is <string>(a => a == "AT+CMGF=1\r"))).Do(c =>
                {
                    _serialPort.ReadExisting().Returns("\r\nOK\r\n");
                    manager._receiveNow.Set();
                });
                _serialPort.When(sp => sp.Write(Arg.Is <string>(a => a == "AT+CPMS?\r"))).Do(c =>
                {
                    _serialPort.ReadExisting().Returns("+CPMS: \"SM\",4,20,\"SM\",0,20,\"ME\",186,1000\r\n\r\nOK\r\n");
                    manager._receiveNow.Set();
                });

                // Act
                int count = manager.CountSMSmessages(_serialPort);

                // Assert
                Assert.AreEqual(4, count);
                // Note that CountSMSmessages() ignores messages not stored on SIM card.
            }