예제 #1
0
        public void Setup_ValueTypeMethodWithArguments_InvokesOriginalMethodWhenNotMatched()
        {
            Smock.Run(context =>
            {
                context.Setup(() => It.IsAny <int>().ToString("NonExistantFormat")).Returns("Test");

                string result = 42.ToString("X");

                Assert.AreEqual("2A", result);
            });
        }
예제 #2
0
        public void Setup_VoidMethod_DoesntInvokeOriginalWhenMatched()
        {
            Smock.Run(context =>
            {
                context.Setup(() => VoidMethod(It.IsAny <string>())).Verifiable();

                VoidMethod("Foo");

                Assert.AreEqual(false, _voidMethodInvoked);
            });
        }
예제 #3
0
 public void CheckDirectoryExistsTest()
 {
     Smock.Run(context =>
     {
         Assert.Throws <ArgumentNullException>(() => ((string)null).CheckDirectoryExists("param"));
         context.Setup(() => Directory.Exists("path")).Returns(false);
         Assert.Throws <DirectoryNotFoundException>(() => "path".CheckDirectoryExists("param"));
         context.Setup(() => Directory.Exists("path")).Returns(true);
         "path".CheckDirectoryExists("param");
     });
 }
예제 #4
0
 public void CheckFileExistsTest()
 {
     Smock.Run(context =>
     {
         Assert.Throws <ArgumentNullException>(() => ((string)null).CheckFileExists("param"));
         context.Setup(() => File.Exists("filename")).Returns(false);
         Assert.Throws <FileNotFoundException>(() => "filename".CheckFileExists("param"));
         context.Setup(() => File.Exists("filename")).Returns(true);
         "filename".CheckFileExists("param");
     });
 }
예제 #5
0
        public void Setup_AnyValueTypeArgument_MatchesAnyInstance()
        {
            Smock.Run(context =>
            {
                context.Setup(() => Enumerable.Range(It.IsAny <int>(), 1)).Returns(new List <int>());

                Assert.AreEqual(0, Enumerable.Range(100, 1).Count());
                Assert.AreEqual(0, Enumerable.Range(200, 1).Count());
                Assert.AreEqual(2, Enumerable.Range(200, 2).Count());
            });
        }
예제 #6
0
        public void Setup_ReferenceTypeArgument_MatchesOnlyEqualInstance()
        {
            Smock.Run(context =>
            {
                context.Setup(() => int.Parse("111")).Returns(10);

                Assert.AreEqual(10, int.Parse("111"));
                Assert.AreEqual(10, int.Parse(new string('1', 3)));
                Assert.AreEqual(100, int.Parse("100"));
            });
        }
예제 #7
0
        public void Setup_ValueTypeTarget_MatchesOnlyEqualInstance(int valueType, int otherValueType)
        {
            Smock.Run(context =>
            {
                const int expected = 1337;

                context.Setup(() => valueType.GetHashCode()).Returns(expected);

                Assert.AreEqual(expected, valueType.GetHashCode());
                Assert.AreNotEqual(expected, otherValueType.GetHashCode());
            });
        }
예제 #8
0
        public void Setup_AnyValueTypeTarget_MatchesAnyInstance()
        {
            Smock.Run(context =>
            {
                const int expected = 1337;

                context.Setup(() => It.IsAny <int>().GetHashCode()).Returns(expected);

                Assert.AreEqual(expected, 1.GetHashCode());
                Assert.AreEqual(expected, 2.GetHashCode());
            });
        }
예제 #9
0
        public void Setup_AnyReferenceTypeTarget_MatchesAnyInstance()
        {
            Smock.Run(context =>
            {
                const int expected = 1337;

                context.Setup(() => It.IsAny <string>().Length).Returns(expected);

                Assert.AreEqual(expected, "Hello".Length);
                Assert.AreEqual(expected, "World".Length);
            });
        }
예제 #10
0
        public void Raise_StaticEvent_RaisesInvokedEvent()
        {
            bool invoked = false;

            Smock.Run(context =>
            {
                Console.CancelKeyPress += (sender, args) => invoked = true;
                context.Raise(() => Console.CancelKeyPress += null, () => Console.CancelKeyPress -= null, default(EventArgs));
            });

            Assert.IsTrue(invoked);
        }
예제 #11
0
 public void Callback_LessThanExpectedTwelveArguments_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         Smock.Run(context =>
         {
             context
             .Setup(() => TestFunctions.TwelveArguments(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))
             .Callback <int, int, int, int, int, int, int, int, int, int, int>((a, b, c, d, e, f, g, h, i, j, k) => { });
         });
     });
 }
예제 #12
0
 public void Callback_MoreThanExpectedOneArgument_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         Smock.Run(context =>
         {
             context
             .Setup(() => TestFunctions.OneArgument(1))
             .Callback <int, int>((a, z) => { });
         });
     });
 }
예제 #13
0
        public void ReturnsCallback_EightMatchingArgs_ReturnsResultFromCallback()
        {
            Smock.Run(context =>
            {
                context
                .Setup(() => TestFunctions.EightArguments(1, 2, 3, 4, 5, 6, 7, 8))
                .Returns <int, int, int, int, int, int, int, int>((a, b, c, d, e, f, g, h) => a + b + c + d + e + f + g + h);

                var result = TestFunctions.EightArguments(1, 2, 3, 4, 5, 6, 7, 8);
                Assert.AreEqual(36, result);
            });
        }
예제 #14
0
 public void ReturnsCallback_TooFewArguments_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         Smock.Run(context =>
         {
             context
             .Setup(() => TestFunctions.TwoArguments(1, 2))
             .Returns <int>(a => 42);
         });
     });
 }
예제 #15
0
        public void ReturnsCallback_FourMatchingArgs_ReturnsResultFromCallback()
        {
            Smock.Run(context =>
            {
                context
                .Setup(() => TestFunctions.FourArguments(1, 2, 3, 4))
                .Returns <int, int, int, int>((a, b, c, d) => a + b + c + d);

                var result = TestFunctions.FourArguments(1, 2, 3, 4);
                Assert.AreEqual(10, result);
            });
        }
예제 #16
0
        public void Setup_MultipleItIsSetups_MatchesTheCorrectSetup()
        {
            Smock.Run(context =>
            {
                context.Setup(() => string.Format(It.Is <string>(format => format.Contains("A")))).Returns("Foo");
                context.Setup(() => string.Format(It.Is <string>(format => format.Contains("B")))).Returns("Bar");

                Assert.AreEqual("Foo", string.Format("AA"));
                Assert.AreEqual("Bar", string.Format("BB"));
                Assert.AreEqual("CC", string.Format("CC"));
            });
        }
예제 #17
0
        public void Callback_SingleConvertibleArg_InvokesCallbackWithArguments()
        {
            Smock.Run(context =>
            {
                int result = 0;
                context.Setup(() => Math.Sqrt(It.IsAny <double>())).Callback <int>(d => result = d * d);

                Math.Sqrt(2.0);

                Assert.AreEqual(4, result);
            });
        }
예제 #18
0
 public void Callback_TooManyVoidMethodArguments_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         Smock.Run(context =>
         {
             context
             .Setup(() => Console.WriteLine("{0}"))
             .Callback <string, int>((a, b) => { });
         });
     });
 }
예제 #19
0
 public void Callback_VoidLessThanExpectedTwoArguments_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         Smock.Run(context =>
         {
             context
             .Setup(() => TestFunctions.VoidTwoArguments(1, 2))
             .Callback <int>((a) => { });
         });
     });
 }
예제 #20
0
 public void Callback_LessThanExpectedSixteenArguments_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         Smock.Run(context =>
         {
             context
             .Setup(() => TestFunctions.SixteenArguments(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16))
             .Callback <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int>((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) => { });
         });
     });
 }
예제 #21
0
 public void ReturnsCallback_TooManyArguments_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         Smock.Run(context =>
         {
             context
             .Setup(() => TestFunctions.OneArgument(1))
             .Returns <int, int>((a, b) => 42);
         });
     });
 }
예제 #22
0
        public void Run_GuidFunc_ReturnsCorrectValue()
        {
            Guid expected = Guid.Parse("{2988B6BD-B4D7-476D-B432-19D781FC9B20}");

            Guid value = Smock.Run(context =>
            {
                context.Setup(() => Guid.Parse(It.IsAny <string>())).Returns(expected);
                return(Guid.Parse(string.Empty));
            });

            Assert.AreEqual(expected, value);
        }
예제 #23
0
        public void ReturnsCallback_SixMatchingArgs_ReturnsResultFromCallback()
        {
            Smock.Run(context =>
            {
                context
                .Setup(() => TestFunctions.SixArguments(1, 2, 3, 4, 5, 6))
                .Returns <int, int, int, int, int, int>((a, b, c, d, e, f) => a + b + c + d + e + f);

                var result = TestFunctions.SixArguments(1, 2, 3, 4, 5, 6);
                Assert.AreEqual(21, result);
            });
        }
예제 #24
0
        public void Run_UriFunc_ReturnsCorrectValue()
        {
            Uri expected = new Uri("http://www.google.com");

            Uri value = Smock.Run(context =>
            {
                context.Setup(() => new Uri(It.IsAny <string>())).Returns(expected);
                return(new Uri(string.Empty));
            });

            Assert.AreEqual(expected, value);
        }
예제 #25
0
        public void NewCombTest()
        {
            DateTime now = DateTime.Now;

            Smock.Run(context =>
            {
                context.Setup(() => DateTime.Now).Returns(now);
                Guid id       = CombHelper.NewComb();
                DateTime time = CombHelper.GetDateFromComb(id);
                Assert.True(time.Subtract(now).TotalSeconds < 1);
            });
        }
예제 #26
0
 public void Callback_VoidMoreThanExpectedThirteenArguments_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         Smock.Run(context =>
         {
             context
             .Setup(() => TestFunctions.VoidThirteenArguments(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13))
             .Callback <int, int, int, int, int, int, int, int, int, int, int, int, int, int>((a, b, c, d, e, f, g, h, i, j, k, l, m, z) => { });
         });
     });
 }
예제 #27
0
        public void Run_ISerializableFunc_ReturnsCorrectValue()
        {
            Exception expected = new Exception("Test");

            Exception value = Smock.Run(context =>
            {
                context.Setup(() => new Exception(It.IsAny <string>())).Returns(expected);
                return(new Exception(string.Empty));
            });

            Assert.AreEqual(expected, value);
        }
예제 #28
0
        public void test()
        {
            Smock.Run(context =>
            {
                context.Setup(() => DateTime.Now).Returns(new DateTime(2000, 1, 1));

                var dateNow = DateTime.Now;
                Assert.That(dateNow, Is.EqualTo(new DateTime(3000, 1, 1)));
                Console.WriteLine("ttu");
                Thread.Sleep(2220);
            });
        }
예제 #29
0
        public void Run_DecimalFunc_ReturnsCorrectValue()
        {
            decimal expected = 1.23m;

            decimal value = Smock.Run(context =>
            {
                context.Setup(() => decimal.Parse(It.IsAny <string>())).Returns(expected);
                return(decimal.Parse("0"));
            });

            Assert.AreEqual(expected, value);
        }
예제 #30
0
 public void Callback_MoreThanExpectedNineArguments_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         Smock.Run(context =>
         {
             context
             .Setup(() => TestFunctions.NineArguments(1, 2, 3, 4, 5, 6, 7, 8, 9))
             .Callback <int, int, int, int, int, int, int, int, int, int>((a, b, c, d, e, f, g, h, i, z) => { });
         });
     });
 }