public void MockStaticDefaultThrowsForAll() { using (var staticMock = new StaticMock(typeof(ClassUsesFile))) { staticMock.For(() => File.ReadAllText("foo.txt")).Returns("bar"); staticMock.ThrowsForAll(new FileNotFoundException()); Assert.That(ClassUsesFile.ShoutFile("foo.txt"), Is.EqualTo("BAR")); Assert.Throws <FileNotFoundException>(() => ClassUsesFile.ShoutFile("boom.txt")); } }
public void MockStaticFunction() { using (var staticMock = new StaticMock(typeof(ClassUsesFile))) { staticMock.For(() => File.ReadAllText("foo.txt")).Returns("bar"); var text = ClassUsesFile.ShoutFile("foo.txt"); Assert.That(text, Is.EqualTo("BAR")); } }
public void MockStaticDefaultThrows() { using (var staticMock = new StaticMock(typeof(ClassUsesFile))) { staticMock.For(() => File.ReadAllText(null)).ReturnsForAnyArgs(x => { switch (x.ArgAt <string>(0)) { case "foo.txt": return("bar"); default: throw new FileNotFoundException(); } }); Assert.That(ClassUsesFile.ShoutFile("foo.txt"), Is.EqualTo("BAR")); Assert.Throws <FileNotFoundException>(() => ClassUsesFile.ShoutFile("boom.txt")); } }