Пример #1
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();
        }
Пример #2
0
        public void VerifyCalls()
        {
            var calculator = Mock.Of <ICalculator>();

            calculator.Setup(c => c.TurnOn()).Once();
            calculator.Add(2, 3).Returns(5).Once();

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

            calculator.TurnOn();
            calculator.Add(2, 3);

            Verify.Calls(calculator);
        }
Пример #3
0
        public void VerifyCallsCustom()
        {
            var calculator = Mock.Of <ICalculator>();

            calculator.Setup(c => c.TurnOn()).Once();
            calculator.Add(2, 3).Returns(5).Once();

            Verify.Calls(
                () => calculator.TurnOn(),
                calls => Assert.Empty(calls));

            calculator.TurnOn();

            Verify.Calls(
                () => calculator.TurnOn(),
                calls => Assert.Single(calls));
        }