[TestMethod] public void RejectCall_UserRecieveCallAndRejectIt_SendReport() { var fakeMessageEngine = A.Fake<IMessageEngine>(); var phone = new Phone(fakeMessageEngine); var notificationService = new NotificationService(phone); var message = new NewCallMessage("otherUserId"); fakeMessageEngine.OnMessageArrived += Raise.With(new MessageArrivedEventArgs(message)).Now; phone.AnswerCall(Answer.Reject); var result = notificationService.GetLastReport(); Assert.AreEqual(ReportType.CallEnded, result.ReportType); }
[TestClass] public class NotificationBddTests { IMessageEngine _fakeMessageEngine; Phone _phone; NotificationService _notificationService; public NotificationBddTests() { _fakeMessageEngine = A.Fake<IMessageEngine>(); _phone = new Phone(_fakeMessageEngine); _notificationService = new NotificationService(_phone); } [TestMethod] public void RejectCall_UserRecieveCallAndRejectIt_SendReport() { new NotificationBddTests() .Given(s => s.RecievedCallFromUser()) .When(s => s.CallRejected()) .Then(s => s.EndCallReportAdded()) .BDDfy(); } private void RecievedCallFromUser() { var message = new NewCallMessage("otherUserId"); _fakeMessageEngine.OnMessageArrived += Raise.With(new MessageArrivedEventArgs(message)).Now; } private void CallRejected() { _phone.AnswerCall(Answer.Reject); } private void EndCallReportAdded() { var result = _notificationService.GetLastReport(); Assert.AreEqual(ReportType.CallEnded, result.ReportType); } }
[TestClass] public class ShoppingCartTests { private readonly ShoppingCart _cart = new ShoppingCart() [TestMethod] public void EmptyCartTest() { new ShoppingCartTests() .Given(s => s.ShoppingCartIsEmpty()) .Then(s => s.TotalPriceEquals(0)) .BDDfy(); } [TestMethod] public void AddItemTest() { var newProduct = new Product(id: "prd-1", price: 100 new ShoppingCartTests() .Given(s => s.ShoppingCartIsEmpty()) .When(s => s.AddProductToCart(newProduct)) .Then(s => s.TotalPriceEquals(newProduct.Price)) .BDDfy(); } private void ShoppingCartIsEmpty() { } private void TotalPriceEquals(int expected) { Assert.AreEqual(expected, _cart.TotalPrice); } private void AddProductToCart(Product product) { _cart.Add(product); } }
public class NotificationTestBuilder { IMessageEngine _fakeMessageEngine; Phone _phone; NotificationService _notificationService; public NotificationTestBuilder() { _fakeMessageEngine = A.Fake<IMessageEngine>(); _phone = new Phone(_fakeMessageEngine); _notificationService = new NotificationService(_phone); } public void RecievedCallFromUser() { var message = new NewCallMessage("otherUserId"); _fakeMessageEngine.OnMessageArrived += Raise.With(new MessageArrivedEventArgs(message)).Now; } public void CallRejected() { _phone.AnswerCall(Answer.Reject); } public void EndCallReportAdded() { var result = _notificationService.GetLastReport(); Assert.AreEqual(ReportType.CallEnded, result.ReportType); } } [TestClass] public class NotificationBddTests { [TestMethod] public void RejectCall_UserRecieveCallAndRejectIt_SendReport() { var builder = new NotificationTestBuilder(); new NotificationBddTests() .Given(s => builder.RecievedCallFromUser()) .When(s => builder.CallRejected()) .Then(s => builder.EndCallReportAdded()) .BDDfy(); } }
Labels: .NET, BDD, C#, MSTest, Unit tests