示例#1
0
        public void TestIReturnsIntTaskMethodStringFallback3()
        {
            var subject = new Mock <IReturnsIntTaskMethodString>();

            subject.Setup(x => x.CreateFile("fool.cs")).Returns(TaskEx.Failed <int>(new NullReferenceException()));
            var fallback = new Mock <IReturnsIntTaskMethodString>();

            fallback.Setup(x => x.CreateFile("fool.cs")).Returns(Task.FromResult(5244222));
            var creator = Create();
            var proxy   = creator.PrepareProxyFor(subject.Object)
                          .WithFallbackTo(fallback.Object)
                          .Create();

            Await(proxy.CreateFile("fool.cs")).Should().Be(5244222);
            subject.Verify(x => x.CreateFile("fool.cs"), Times.Once);
            fallback.Verify(x => x.CreateFile("fool.cs"), Times.Once);
        }
示例#2
0
        public void TestIReturnTaskFallback5()
        {
            var subject = new Mock <IReturnsTask>();

            subject.Setup(x => x.DoStuff()).Returns(TaskEx.Failed <int>(new NullReferenceException()));
            var fallback = new Mock <IReturnsTask>();

            fallback.Setup(x => x.DoStuff()).Returns(TaskEx.Failed <int>(new FileNotFoundException()));
            var creator = Create();
            var proxy   = creator.PrepareProxyFor(subject.Object)
                          .WithFallbackTo(fallback.Object)
                          .Create();

            new Action(() => Await(proxy.DoStuff()))
            .ShouldThrow <AggregateException>()
            .WithInnerException <FileNotFoundException>();
            subject.Verify(x => x.DoStuff(), Times.Once);
            fallback.Verify(x => x.DoStuff(), Times.Once);
        }
示例#3
0
        public void TestIReturnsIntTaskMethodString4()
        {
            var subject = new Mock <IReturnsIntTaskMethodString>();

            subject.Setup(x => x.CreateFile("stalker shadow of chernobyl")).Returns(TaskEx.Failed <int>(new NullReferenceException()));
            var fallback = new Mock <IReturnsIntTaskMethodString>();

            fallback.Setup(x => x.CreateFile("stalker shadow of chernobyl")).Throws <FileNotFoundException>();
            var creator = Create();
            var proxy   = creator.PrepareProxyFor(subject.Object)
                          .WithFallbackTo(fallback.Object)
                          .Create();

            new Action(() => Await(proxy.CreateFile("stalker shadow of chernobyl")))
            .ShouldThrow <AggregateException>()
            .WithInnerException <FileNotFoundException>();
            subject.Verify(x => x.CreateFile("stalker shadow of chernobyl"), Times.Once);
            fallback.Verify(x => x.CreateFile("stalker shadow of chernobyl"), Times.Once);
        }
示例#4
0
        public void TestIReturnsIntTaskDefaultFallback()
        {
            var subject = new Mock <IReturnsIntTask>();
            var creator = Create();
            var proxy   = creator.PrepareProxyFor(subject.Object)
                          .WithDefaultFallback()
                          .Create();

            Save();

            proxy.DoStuff();
            subject.Verify(x => x.DoStuff(), Times.Once);

            subject.Setup(x => x.DoStuff()).Throws <ArithmeticException>();
            new Action(() => proxy.DoStuff().Wait()).ShouldNotThrow();
            subject.Verify(x => x.DoStuff(), Times.Exactly(2));

            subject.Setup(x => x.DoStuff()).Returns(TaskEx.Failed <int>(new NullReferenceException()));
            proxy.DoStuff().Result.Should().Be(0);
        }
示例#5
0
        public void TestIReturnsTaskFallback3()
        {
            var subject = new Mock <IReturnsTask>();

            subject.Setup(x => x.DoStuff()).Returns(TaskEx.Failed <int>(new NullReferenceException()));
            var source   = new TaskCompletionSource <int>();
            var fallback = new Mock <IReturnsTask>();

            fallback.Setup(x => x.DoStuff()).Returns(source.Task);
            var creator = Create();
            var proxy   = creator.PrepareProxyFor(subject.Object)
                          .WithFallbackTo(fallback.Object)
                          .Create();

            var task = proxy.DoStuff();

            task.Wait(TimeSpan.FromMilliseconds(10)).Should().BeFalse("because the actual task didn't finished just yet");

            source.SetResult(42);
            Await(task);
            subject.Verify(x => x.DoStuff(), Times.Once);
            fallback.Verify(x => x.DoStuff(), Times.Once);
        }