コード例 #1
0
    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"));
        }
    }
コード例 #2
0
    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"));
        }
    }
コード例 #3
0
    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"));
        }
    }