public object StaticMock_For_NoReturns() { using (var staticMock = new StaticMock(typeof(Program))) { staticMock.For(() => System.IO.File.ReadAllText("foo")); return(Program.Read("foo")); } }
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 ValidateStaticAction() { using (var staticMock = new StaticMock(typeof(ClassUsesConsole))) { var text = "Hello, World!"; staticMock.For(() => Console.WriteLine(Arg.Any <string>())); ClassUsesConsole.WriteLine(text); staticMock.Received(1, () => Console.WriteLine(text)); } }
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")); } }
public void ResolveAssembly_NewTimeStamp_AssemblyChanged() { var testAssembly = GetType().Assembly; var assemblyName = testAssembly.GetName().Name; var assemblyFile = testAssembly.Location; using (var ghostAssemblyLoader = new GhostAssemblyLoader(assemblyFile, assemblyName)) { staticMock.For(() => File.GetLastWriteTime(assemblyFile)).Returns(File.GetLastWriteTime(assemblyFile)); var asm1 = ghostAssemblyLoader.ResolveAssembly(); staticMock.For(() => File.GetLastWriteTime(assemblyFile)).Returns(DateTime.Now); var asm2 = ghostAssemblyLoader.ResolveAssembly(); Assert.That(asm2, Is.Not.EqualTo(asm1)); } }