public void ShouldThrowException() { var mockIlist = new Mock <IList>(); mockIlist.Setup(x => x.Add(It.Is <string>(s => s.Length >= 10))) .Throws <ArgumentException>(); var sut = new DemoServiceWithDependency(mockIlist.Object); Assert.That(() => sut.Add("impossible"), Throws.Exception); }
public void ShouldVerifyGet() { var mockIlist = new Mock <IList>(); mockIlist.Setup(x => x.Count).Returns(123); var sut = new DemoServiceWithDependency(mockIlist.Object); var length = sut.GetLength(); Assert.That(length, Is.EqualTo(123)); mockIlist.VerifyGet(x => x.Count); }
public void ShouldVerifyMethodArgs() { string param = "I am not out there!"; var mockIlist = new Mock <IList>(); mockIlist.Setup(x => x.Contains(It.IsAny <string>())).Returns(false); var sut = new DemoServiceWithDependency(mockIlist.Object); var result = sut.Contains(param); mockIlist.Verify(x => x.Contains(It.Is <string>(s => s.Equals(param)))); }
public void ShouldNotContain() { var _mockIlist = new Mock <IList>(); _mockIlist.Setup(x => x.Contains(It.IsAny <string>())).Returns(false); var sut = new DemoServiceWithDependency(_mockIlist.Object); var tricky = "I am not out there!"; sut.Add(tricky); var result = sut.Contains(tricky); Assert.That(result, Is.EqualTo(false)); }
public void ShouldReturnDifferentValues() { var _mockIlist = new Mock <IList>(); bool contains = false; _mockIlist.Setup(x => x.Contains(It.IsAny <string>())) .Returns(() => contains) .Callback(() => contains = !contains); var sut = new DemoServiceWithDependency(_mockIlist.Object); var result1 = sut.Contains("test value"); var result2 = sut.Contains("test value"); Assert.That(result1, Is.Not.EqualTo(result2)); _mockIlist.Verify(); }
public void Init() { _mockIlist = new Mock <IList>(); _mockIlist.Setup(x => x.Add(It.IsAny <object>())).Verifiable(); _sut = new DemoServiceWithDependency(_mockIlist.Object); }