Пример #1
0
        public void YouCanCheckToSeeIfAMethodWasCalled()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Call some method.
            stub.MethodThatReturnsInteger("foo");

            // Check that particular method was called.
            stub.AssertWasCalled(s => s.MethodThatReturnsInteger("foo"));
            stub.AssertWasCalled(s => s.MethodThatReturnsInteger(Arg <string> .Is.Anything));
        }
Пример #2
0
        public void YouCanCheckToSeeIfAMethodWasCalledACertainNumberOfTimes()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Call some methods.
            stub.MethodThatReturnsInteger("foo");
            stub.MethodThatReturnsInteger("bar");

            // This will pass.
            stub.AssertWasCalled(s => s.MethodThatReturnsInteger("foo"), o => o.Repeat.Once());

            // Call the method a second time.
            stub.MethodThatReturnsInteger("foo");

            // Now this will fail because we called it a second time.
            Assert.Throws <ExpectationViolationException>(() => stub.AssertWasCalled(s => s.MethodThatReturnsInteger("foo"), o => o.Repeat.Once()));

            // Some other options.
            stub.AssertWasCalled(s => s.MethodThatReturnsInteger("foo"), o => o.Repeat.Times(2));
            stub.AssertWasCalled(s => s.MethodThatReturnsInteger("foo"), o => o.Repeat.AtLeastOnce());
            stub.AssertWasCalled(s => s.MethodThatReturnsInteger("foo"), o => o.Repeat.Twice());
        }