public void Test_WriteLine_Calls_Write_With_Appropriate_Argument() { // Arrange var mock = new Mock<ILogWriter>(); var logger = new Logger(mock.Object); // Act logger.WriteLine("Hello, logger!"); // Assert // Checking that Write method was called with appropriate argument mock.Verify(lw => lw.Write("Hello, logger!")); }
public void Test_WriteLine_Calls_Write() { // Arrange var mock = new Mock<ILogWriter>(); var logger = new Logger(mock.Object); // Act logger.WriteLine("Hello, logger!"); // Assert // Checking that Write method of the ILogWriter was called mock.Verify(lw => lw.Write(It.IsAny<string>())); }
public void Test_WriteLine_Called_Exactly_Once() { // Arrange var mock = new Mock<ILogWriter>(); var logger = new Logger(mock.Object); // Act logger.WriteLine("Hello, logger!"); // Assert // We could check, that particular method calls specified number of times mock.Verify(lw => lw.Write(It.IsAny<string>()), Times.Once()); }
public void Test_WriteLine_Calls_Write_With_Setup_Method() { // Arrange var mock = new Mock<ILogWriter>(); mock.Setup(lw => lw.Write(It.IsAny<string>())); var logger = new Logger(mock.Object); // Act logger.WriteLine("Hello, logger!"); // Assert // We're not explicitly stated what we're expecting. // mock.Setup expectations would be use. mock.Verify(); }
public void Test_WriteLine_And_SetLogger_Calls() { // By default Moq uses loose verification model, so verify method will // passed even if we're not explicitly set up all required dependencies. // In strict mode Verify method will fail if we left some calls in setup method. // Arrange var mock = new Mock<ILogWriter>(MockBehavior.Strict); // Commenting one of the following lines will lead to exception in mock.Verify() mock.Setup(lw => lw.Write(It.IsAny<string>())); mock.Setup(lw => lw.SetLogger(It.IsAny<string>())); var logger = new Logger(mock.Object); // Act logger.WriteLine("Hello, logger!"); // Assert mock.Verify(); }