public void it_should_pass_data_to_file_writer() { // arrange var order = new Order {OrderId = 1001, OrderTotal = 10.53M}; var mockFileWriter = new Mock<IFileWriter>(); var orderWriter = new OrderWriter(mockFileWriter.Object); // act orderWriter.WriteOrder(order); // assert mockFileWriter.Verify(fw => fw.WriteLine("1001,10.53"), Times.Exactly(1)); // method is not executed, expression tree is veryfied mockFileWriter.Verify(fw => fw.WriteLine(It.IsAny<string>()), Times.Once()); // use regex mockFileWriter.Verify(fw => fw.WriteLine(It.IsRegex("^1001")), Times.Once()); // use predicate mockFileWriter.Verify(fw => fw.WriteLine(It.Is<string>(s => s.Length > 3)), Times.Once()); mockFileWriter.Verify(fw => fw.WriteLine(It.Is<string>(s => s.StartsWith("1001,10.53"))), Times.Once()); // can use local variable string expectedValue = "1001,10.53"; mockFileWriter.Verify(fw => fw.WriteLine(It.Is<string>(s => s.StartsWith(expectedValue))), Times.Once()); // verify property mockFileWriter.VerifySet(fw => fw.FileName = It.IsAny<string>()); mockFileWriter.VerifySet(fw => fw.FileName = "1001.txt"); mockFileWriter.VerifySet(fw => fw.FileName = It.Is<string>(s => s.StartsWith("1001"))); }
public void it_should_pass_data_to_file_writer2() { // arrange var order = new Order { OrderId = 1001, OrderTotal = 10.53M }; var mockFileWriter = new Mock<IFileWriter>(); mockFileWriter.Setup(fw => fw.WriteLine("1001,10.53")); var orderWriter = new OrderWriter(mockFileWriter.Object); // act orderWriter.WriteOrder(order); // assert mockFileWriter.Verify(fw => fw.WriteLine(It.IsAny<string>()), Times.AtMostOnce()); }