Esempio n. 1
0
        public void VerifyPropertySet()
        {
            var calculator = Mock.Of <ICalculator>();

            Assert.Throws <VerifyException>(() => Verify.Called(() => calculator.Mode = CalculatorMode.Scientific));

            calculator.Mode = CalculatorMode.Scientific;

            Verify.Called(() => calculator.Mode = CalculatorMode.Scientific);
        }
Esempio n. 2
0
        //[Fact]
        internal void CanVerify()
        {
            var calculator = Mock.Of <ICalculator>();

            calculator.Add(2, 3).Returns(5).Once();
            calculator.Mode.Returns(CalculatorMode.Scientific).Exactly(2);

            var mock = Mock.Get(calculator);

            foreach (var invocation in mock.Invocations)
            {
                output.WriteLine((string)invocation.Context[nameof(Environment.StackTrace)]);
            }

            // Syntax-based, follows straightforward setup approach, no lambdas.
            Verify(calculator).Mode = CalculatorMode.Scientific;
            Verify(calculator).Add(2, 3);

            // Verify all "verifiable" calls, i.e. those with
            // a Once/Never/etc. setup.
            Verify(calculator);
            // Long form, with no lambda
            Verify.Called(calculator);

            // equivalent non-syntax version
            //calculator.Verify().Add(2, 3);
            Verify(calculator).Add(1, 1).Never();

            // Explicit Verify, still no lambdas, long form of Syntax
            Verify.Called(calculator).Add(2, 3).Exactly(2);
            // Explicit Verify, still no lambdas, long form of Syntax
            Verify.NotCalled(calculator).Add(2, 3);

            // For the case where you want keep the mock in "running" mode after the verify.

            Verify.Called(() => calculator.Add(2, 3).Once());
            Verify.NotCalled(() => calculator.TurnOn());

            // More advanced verification, access calls via lambda
            Verify.Calls(
                () => calculator.Add(2, 3),
                calls => Assert.Single(calls));
            // Works for void/action
            Verify.Calls(
                () => calculator.TurnOn(),
                calls => Assert.Single(calls));

            // Verify all "verifiable" calls, i.e. those with
            // a Once/Never/etc. setup.
            Verify.Calls(calculator);
            // calculator.Verify();
        }
Esempio n. 3
0
        public void VerifyVoidMethod()
        {
            var calculator = Mock.Of <ICalculator>();

            Assert.Throws <VerifyException>(() => Verify.Called(() => calculator.TurnOn()));

            calculator.TurnOn();

            Verify.Called(() => calculator.TurnOn());

            Assert.Throws <VerifyException>(() => Verify.Called(() => calculator.TurnOn(), 2));

            calculator.TurnOn();

            Verify.Called(() => calculator.TurnOn(), 2);
        }