public void PingFailed() { string[] hosts = new String[] { "google" }; Mock <IConfigController> mockConfigControler = new Mock <IConfigController>(); mockConfigControler.SetupGet(cfg => cfg.Config).Returns(new DTO.ConfigDTO() { Hosts = hosts.ToList() }); Mock <IPing> mockPing = new Mock <IPing>(); mockPing.Setup(p => p.Send(It.IsNotNull <string>(), It.IsAny <int>(), It.IsAny <byte[]>(), It.IsAny <PingOptions>())).Returns(new PingReplyFacade() { Status = IPStatus.TimedOut }); Mock <IPingFactory> mockPingFactory = new Mock <IPingFactory>(); mockPingFactory.Setup(pf => pf.CreateNewPing()).Returns(mockPing.Object); IPingController pingController = new PingController(mockConfigControler.Object, mockPingFactory.Object); IList <PingResultDTO> result = pingController.DoForAll(); Assert.IsNotNull(result.First()); Assert.IsFalse(result.First().Success); }
public void NumberOfExecutionsDoItAll() { string[] hosts = new String[] { "google", "ugz", "polishdev.com" }; Mock <IConfigController> mockConfigControler = new Mock <IConfigController>(); mockConfigControler.SetupGet(cfg => cfg.Config).Returns(new DTO.ConfigDTO() { Hosts = hosts.ToList() }); Mock <IPing> mockPing = new Mock <IPing>(); int numberOfPingExecutions = 0; mockPing.Setup(p => p.Send(It.IsNotNull <string>(), It.IsAny <int>(), It.IsAny <byte[]>(), It.IsAny <PingOptions>())).Returns(new PingReplyFacade() { Status = IPStatus.Success }).Callback(() => { numberOfPingExecutions++; }); Mock <IPingFactory> mockPingFactory = new Mock <IPingFactory>(); mockPingFactory.Setup(pf => pf.CreateNewPing()).Returns(mockPing.Object); IPingController pingController = new PingController(mockConfigControler.Object, mockPingFactory.Object); IList <PingResultDTO> result = pingController.DoForAll(); Assert.AreEqual(hosts.Count(), numberOfPingExecutions); }