Ejemplo n.º 1
0
        public void CheckForAcceptableTypes()
        {
            using (CaptureConsole) {
                var x = new ActualImplementation().As <ClientInterface>();
                var y = x.ActuallyReturnsString();
                var z = x.ActuallyReturnsFileStream();

                Console.WriteLine("Y is {0}", y.GetType().Name);
                Console.WriteLine("Z is {0}", z.GetType().Name);

                // this function doesn't match anything in the implemention
                // so a stub method gets created (which returns null)
                // MemoryStream a = x.ActuallyRetunsMemoryStream();
                // Assert.Null(a);

                // the clientinterface is more restricted than the implementation
                // but that's ok.
                MemoryStream ms = new MemoryStream();
                Assert.True(x.TakesAStream(ms));

                // the clientinterface is less restrictive than the implementation
                // and that's not ok.
                Assert.False(x.TakesAFileStream(ms));

                var shouldWork = new {
                    TakesAStream = new Func <Stream, bool>(stream => { return(stream != null); })
                }.As <ClientInterface>();

                Assert.True(shouldWork.TakesAStream(ms));

                var shouldNotWork = new {
                    TakesAFileStream = new Func <MemoryStream, bool>(stream => {
                        Console.WriteLine("never called");
                        return(stream != null);
                    })
                }.As <ClientInterface>();

                Assert.False(shouldWork.TakesAFileStream(ms));

                var shouldWorkToo = new {
                    ActuallyReturnsString = new Func <object>(() => "hello")
                }.As <ClientInterface>();

                Assert.NotNull(shouldWorkToo.ActuallyReturnsString());

                var shouldNotWorkToo = new {
                    ActuallyRetunsMemoryStream = new Func <Stream>(() => new MemoryStream())
                }.As <ClientInterface>();

                Assert.Null(shouldNotWorkToo.ActuallyRetunsMemoryStream());

                Func <object> fReturnsAString = new Func <object>(() => "hello");

                var fShouldWork = fReturnsAString.As <ReturnsAnObject>();

                Assert.NotNull(fShouldWork());

                Assert.Throws <Exception>(() => {
                    // this shouldn't work because the return type object
                    // can't be expressed as a string.
                    var fShouldNotWork = fReturnsAString.As <ReturnsAString>();
                });
            }
        }
Ejemplo n.º 2
0
        public void CheckForAcceptableTypes()
        {
            using (CaptureConsole) {
                var x = new ActualImplementation().As<ClientInterface>();
                var y = x.ActuallyReturnsString();
                var z = x.ActuallyReturnsFileStream();

                Console.WriteLine("Y is {0}", y.GetType().Name);
                Console.WriteLine("Z is {0}", z.GetType().Name);

                // this function doesn't match anything in the implemention
                // so a stub method gets created (which returns null)
                // MemoryStream a = x.ActuallyRetunsMemoryStream();
                // Assert.Null(a);

                // the clientinterface is more restricted than the implementation
                // but that's ok.
                MemoryStream ms = new MemoryStream();
                Assert.True(x.TakesAStream(ms));

                // the clientinterface is less restrictive than the implementation
                // and that's not ok.
                Assert.False(x.TakesAFileStream(ms));

                var shouldWork = new {
                    TakesAStream = new Func<Stream, bool>(stream => {return stream != null;})
                }.As<ClientInterface>();

                Assert.True(shouldWork.TakesAStream(ms));

                var shouldNotWork = new {
                    TakesAFileStream = new Func<MemoryStream, bool>(stream => {
                        Console.WriteLine("never called");
                        return stream != null;
                    })
                }.As<ClientInterface>();

                Assert.False(shouldWork.TakesAFileStream(ms));

                var shouldWorkToo = new {
                    ActuallyReturnsString = new Func<object>(() => "hello")
                }.As<ClientInterface>();

                Assert.NotNull(shouldWorkToo.ActuallyReturnsString());

                var shouldNotWorkToo = new {
                    ActuallyRetunsMemoryStream = new Func<Stream>(() => new MemoryStream())
                }.As<ClientInterface>();

                Assert.Null(shouldNotWorkToo.ActuallyRetunsMemoryStream());

                Func<object> fReturnsAString = new Func<object>(() => "hello");

                var fShouldWork = fReturnsAString.As<ReturnsAnObject>();

                Assert.NotNull(fShouldWork());

                Assert.Throws<Exception>(() => {
                    // this shouldn't work because the return type object
                    // can't be expressed as a string.
                    var fShouldNotWork = fReturnsAString.As<ReturnsAString>();
                });
            }
        }