Exemplo n.º 1
0
        public void PassParametersToAllAddedMethods()
        {
            var input = "My input string";

            _mockInterface1.Setup(i => i.CountLetters(It.Is <string>(s => s == input)))
            .Verifiable();

            _mockInterface2.Setup(i => i.CountLetters(It.Is <string>(s => s == input)))
            .Verifiable();

            // Create two delegate instances and use +operator to combine them
            //CountLettersDelegate del1 = _mockInterface1.Object.CountLetters;
            //CountLettersDelegate del2 = _mockInterface2.Object.CountLetters;
            // create a multicast delegate that calls both methods
            //var mcd = del1 + del2;

            // OR create a delegate and use +=operator to add other method
            var mcd = new CountLettersDelegate(_mockInterface1.Object.CountLetters);

            mcd += _mockInterface2.Object.CountLetters;

            var output = mcd(input);

            // make sure both mock methods were called
            _mockInterface1.Verify();
            _mockInterface2.Verify();
        }
Exemplo n.º 2
0
        public void WorkWithAnonymousMethodsToo()
        {
            CountLettersDelegate del = (string s) => { return(s.Length * s.Length); };

            var input = "Howdy!!!";

            Assert.Equal(input.Length * input.Length, del(input));
        }
Exemplo n.º 3
0
        public void WorkWithStaticMethodsToo()
        {
            var del = new CountLettersDelegate(SomeStaticClass.CountLetters);

            var input = "Howdy!!!";

            Assert.Equal(input.Length, del(input));
        }
Exemplo n.º 4
0
        public void ReturnValueFromMethod()
        {
            var input = "My input string";

            _mockInterface.Setup(i => i.CountLetters(input))
            .Returns(input.Length);

            var del = new CountLettersDelegate(_mockInterface.Object.CountLetters);

            var result = del(input);

            // make sure the delegate returns the same value as the method
            Assert.Equal(input.Length, result);
        }
Exemplo n.º 5
0
        public void PassParametersToMethod()
        {
            var input = "My input string";

            _mockInterface.Setup(i => i.CountLetters(It.Is <string>(s => s == input)))
            .Verifiable();

            // CountLettersDelegate d = _mockInterface.Object.CountLetters;
            // OR
            var del = new CountLettersDelegate(_mockInterface.Object.CountLetters);

            del(input);

            // make sure the delegate called the method on the mock
            _mockInterface.Verify();
        }
Exemplo n.º 6
0
        public void ReturnValueFromLastInvokedMethod()
        {
            var input = "My input string";

            _mockInterface1.Setup(i => i.CountLetters(It.Is <string>(s => s == input)))
            .Returns(1);

            _mockInterface2.Setup(i => i.CountLetters(It.Is <string>(s => s == input)))
            .Returns(2);

            CountLettersDelegate del1 = _mockInterface1.Object.CountLetters;
            CountLettersDelegate del2 = _mockInterface2.Object.CountLetters;

            // create a multicast delegate that calls both methods
            var mcd = del1 + del2;

            var output = mcd(input);

            // make sure return value from last method is returned
            Assert.Equal(2, output);
        }
        public void NotInvokeRemovedMethods()
        {
            var input = "My input string";

            _mockInterface1.Setup(i => i.CountLetters(It.Is<string>(s => s == input)))
                           .Verifiable();

            _mockInterface2.Setup(i => i.CountLetters(It.Is<string>(s => s == input)))
                           .Verifiable();

            CountLettersDelegate del = _mockInterface1.Object.CountLetters;
            del += _mockInterface2.Object.CountLetters;

            // remove the method from mock interface 1
            del -= _mockInterface1.Object.CountLetters;

            var output = del(input);

            // make sure mock interface 1 method wasn't called and 2 was called
            _mockInterface1.Verify(i => i.CountLetters(It.IsAny<string>()), Times.Never);
            _mockInterface2.Verify();
        }