Example #1
0
        public void YouCanDoArbitraryStuffWhenAMethodIsCalled()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Prepare stub method.
            stub.MethodThatReturnsInteger(Arg.Any <string>()).Returns(s => int.Parse(s.Arg <string>()));

            // Check that method returns proper values.
            stub.MethodThatReturnsInteger("3").Should(Be.EqualTo(3));
            stub.MethodThatReturnsInteger("6").Should(Be.EqualTo(6));
        }
Example #2
0
        public void YouCanTellTheStubWhatValueToReturnWhenIsMethodIsCalledWithAnyArgument()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Arrange stub method.
            stub.Stub(s => s.MethodThatReturnsInteger(Arg <string> .Is.Anything)).Return(5);

            // Now it doesn't matter what the parameter is, we'll always get 5.
            stub.MethodThatReturnsInteger("foo").Should(Be.EqualTo(5));
            stub.MethodThatReturnsInteger("bar").Should(Be.EqualTo(5));
            stub.MethodThatReturnsInteger(null).Should(Be.EqualTo(5));
        }
Example #3
0
        public void YouCanTellTheStubWhatValueToReturnWhenIsMethodIsCalledWithSpecificArguments()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Arrange stub method.
            stub.Stub(s => s.MethodThatReturnsInteger("foo")).Return(5);

            // Calling the method with "foo" as the parameter will return 5.
            stub.MethodThatReturnsInteger("foo").Should(Be.EqualTo(5));

            // Calling the method with anything other than "foo" as the parameter will return the default value.
            stub.MethodThatReturnsInteger("bar").Should(Be.EqualTo(0));
        }
Example #4
0
        public void YouCanTellTheStubToThrowAnExceptionWhenAMethodIsCalled()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Calling the void method will throw exception.
            stub.When(s => s.VoidMethod()).Do(x => { throw new InvalidOperationException(); });
            // Calling the method with "foo" as the parameter will throw exception.
            stub.MethodThatReturnsInteger("foo").Returns(x => { throw new InvalidOperationException(); });

            // Call method that throws exception.
            Assert.Throws <InvalidOperationException>(stub.VoidMethod);
            Assert.Throws <InvalidOperationException>(() => stub.MethodThatReturnsInteger("foo"));
        }
Example #5
0
        public void YouCanTellTheStubWhatValueToReturnWhenIsMethodIsCalledWithAnyArgument()
        {
            // Create a stub.
            ISampleClass        stub      = CreateStub();
            Mock <ISampleClass> stubClass = Mock.Get(stub);

            // Arrange stub method.
            stubClass.Setup(s => s.MethodThatReturnsInteger(It.IsAny <string>())).Returns(5);

            // Now it doesn't matter what the parameter is, we'll always get 5.
            stub.MethodThatReturnsInteger("foo").Should(Be.EqualTo(5));
            stub.MethodThatReturnsInteger("bar").Should(Be.EqualTo(5));
            stub.MethodThatReturnsInteger(null).Should(Be.EqualTo(5));
        }
Example #6
0
        public void YouCanGetTheArgumentsOfCallsToAMethod()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

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

            // GetArgumentsForCallsMadeOn() returns a list of arrays that contain
            // the parameter values for each call to the method.
            IList <object[]> argsPerCall = stub.GetArgumentsForCallsMadeOn(s => s.MethodThatReturnsInteger(null));

            argsPerCall[0][0].Should(Be.EqualTo("foo"));
            argsPerCall[1][0].Should(Be.EqualTo("bar"));
        }
Example #7
0
        public void YouCanGetFancyWithParametersInStubs()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Arg<>.Matches() allows us to specify a lambda expression that specifies
            // whether the return value should be used in this case.  Here we're saying
            // that we'll return 5 if the string passed in is longer than 2 characters.
            stub.Stub(s => s.MethodThatReturnsInteger(Arg <string> .Matches(arg => arg != null && arg.Length > 2))).Return(5);

            // Call method with different parameters.
            stub.MethodThatReturnsInteger("fooo").Should(Be.EqualTo(5));
            stub.MethodThatReturnsInteger("foo").Should(Be.EqualTo(5));
            stub.MethodThatReturnsInteger("fo").Should(Be.EqualTo(0));
            stub.MethodThatReturnsInteger("f").Should(Be.EqualTo(0));
            stub.MethodThatReturnsInteger(null).Should(Be.EqualTo(0));
        }
Example #8
0
        public void CallingMethodsThatReturnValueTypesWillReturnTheDefaultValueForThatType()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Call method that returns value type.
            stub.MethodThatReturnsInteger("foo").Should(Be.EqualTo(0));
        }
Example #9
0
        public void YouCanTellTheStubToReturnValuesInALazyWay()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            int[] count = { 0 };

            // Arrange stub method.
            stub.MethodThatReturnsInteger(Arg.Any <string>()).Returns(s => count[0]);

            // Calling the method will return 1.
            count[0]++;
            stub.MethodThatReturnsInteger("foo").Should(Be.EqualTo(1));

            // Calling the method will return 2.
            count[0]++;
            stub.MethodThatReturnsInteger("bar").Should(Be.EqualTo(2));
        }
Example #10
0
        public void YouCanDoArbitraryStuffWhenAMethodIsCalled()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Prepare stub method.
            stub.Stub(s => s.MethodThatReturnsInteger(Arg <string> .Is.Anything))
            .Return(0)
            .WhenCalled(method =>
            {
                var param          = (string)method.Arguments[0];
                method.ReturnValue = int.Parse(param);
            });

            // Check that method returns proper values.
            stub.MethodThatReturnsInteger("3").Should(Be.EqualTo(3));
            stub.MethodThatReturnsInteger("6").Should(Be.EqualTo(6));
        }
Example #11
0
        public void YouCanCheckToSeeIfAMethodWasCalled()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

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

            // Check that particular method was called.
            stub.Received().MethodThatReturnsInteger("foo");
            stub.ReceivedWithAnyArgs().MethodThatReturnsInteger(Arg.Any <string>());
        }
Example #12
0
        public void YouCanDoArbitraryStuffWhenAMethodIsCalled()
        {
            // Create a stub.
            ISampleClass        stub      = CreateStub();
            Mock <ISampleClass> stubClass = Mock.Get(stub);

            bool before = false;
            bool after  = false;

            // Prepare stub method.
            stubClass.Setup(s => s.MethodThatReturnsInteger(It.IsAny <string>()))
            .Callback(() => { before = true; })
            .Returns <string>(int.Parse)
            .Callback <string>(s => { after = true; });

            // Check that method returns proper values.
            stub.MethodThatReturnsInteger("3").Should(Be.EqualTo(3));
            stub.MethodThatReturnsInteger("6").Should(Be.EqualTo(6));
            before.Should(Be.True);
            after.Should(Be.True);
        }
Example #13
0
        public void YouCanCheckToSeeIfAMethodWasCalledACertainNumberOfTimes()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

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

            // This will pass.
            stub.Received(1).MethodThatReturnsInteger("foo");

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

            // Now this will fail because we called it a second time.
            Assert.Throws <ReceivedCallsException>(() => stub.Received(1).MethodThatReturnsInteger("foo"));

            // Some other options.
            stub.Received(2).MethodThatReturnsInteger("foo");
        }
Example #14
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));
        }
Example #15
0
        public void YouCanCheckToSeeIfAMethodWasNotCalled()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

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

            // Check that other methods were not called.
            stub.AssertWasNotCalled(s => s.MethodThatReturnsInteger("asdfdsf"));
            stub.AssertWasNotCalled(s => s.MethodThatReturnsObject(Arg <int> .Is.Anything));
            stub.AssertWasNotCalled(s => s.VoidMethod());
        }
Example #16
0
        public void YouCanCheckToSeeIfAMethodWasCalled()
        {
            // Create a stub.
            ISampleClass        stub      = CreateStub();
            Mock <ISampleClass> stubClass = Mock.Get(stub);

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

            // Check that particular method was called.
            stubClass.Verify(s => s.MethodThatReturnsInteger("foo"));
            stubClass.Verify(s => s.MethodThatReturnsInteger(It.IsAny <string>()));
        }
Example #17
0
        public void YouCanCheckToSeeIfAMethodWasNotCalled()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

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

            // Check that other methods were not called.
            stub.DidNotReceive().MethodThatReturnsInteger("asdfdsf");
            stub.DidNotReceiveWithAnyArgs().MethodThatReturnsObject(Arg.Any <int>());
            stub.DidNotReceive().VoidMethod();
        }
Example #18
0
        public void YouCanCheckToSeeIfAMethodWasNotCalled()
        {
            // Create a stub.
            ISampleClass        stub      = CreateStub();
            Mock <ISampleClass> stubClass = Mock.Get(stub);

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

            // Check that other methods were not called.
            Assert.Throws <MockException>(() => stubClass.Verify(s => s.MethodThatReturnsInteger("asdfdsf")));
            Assert.Throws <MockException>(() => stubClass.Verify(s => s.MethodThatReturnsObject(It.IsAny <int>())));
            Assert.Throws <MockException>(() => stubClass.Verify(s => s.VoidMethod()));
        }
Example #19
0
        public void YouCanCheckToSeeIfAMethodWasCalledACertainNumberOfTimes()
        {
            // Create a stub.
            ISampleClass        stub      = CreateStub();
            Mock <ISampleClass> stubClass = Mock.Get(stub);

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

            // This will pass.
            stubClass.Verify(s => s.MethodThatReturnsInteger("foo"), Times.Once());

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

            // Now this will fail because we called it a second time.
            Assert.Throws <MockException>(() => stubClass.Verify(s => s.MethodThatReturnsInteger("foo"), Times.Once()));

            // Some other options.
            stubClass.Verify(s => s.MethodThatReturnsInteger("foo"), Times.Exactly(2));
            stubClass.Verify(s => s.MethodThatReturnsInteger("foo"), Times.AtLeastOnce());
        }
Example #20
0
        public void YouCanTellTheStubToReturnValuesInALazyWay()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            int[] count = { 0 };

            // Arrange stub method.
            stub.Stub(s => s.MethodThatReturnsInteger(Arg <string> .Is.Anything))
            .Return(0)
            .WhenCalled(method =>
            {
                method.ReturnValue = count[0];
            });

            // Calling the method will return 1.
            count[0]++;
            stub.MethodThatReturnsInteger("foo").Should(Be.EqualTo(1));

            // Calling the method will return 2.
            count[0]++;
            stub.MethodThatReturnsInteger("bar").Should(Be.EqualTo(2));
        }
Example #21
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());
        }
Example #22
0
        public void YouCanClearReceivedCalls()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

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

            // Check that method was called.
            stub.Received().MethodThatReturnsInteger("foo");

            stub.ClearReceivedCalls();

            // Check that method was not called.
            stub.DidNotReceiveWithAnyArgs().MethodThatReturnsInteger(Arg.Any <string>());
        }
Example #23
0
        public void YouCanCreateStubByCallingMockRepositoryGenerateStub()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Ensure that its string property is null or empty.
            stub.Property.Should(Be.Null.Or.Empty);

            int    outparam;
            string refparam = "test";

            // Call some methods.
            stub.VoidMethod();
            stub.MethodThatReturnsInteger("test").Should(Be.EqualTo(0));
            stub.MethodThatReturnsObject(0).Should(Be.Null);
            stub.MethodWithOutParameter(out outparam);
            outparam.Should(Be.EqualTo(0));
            stub.MethodWithRefParameter(ref refparam);
            refparam.Should(Be.EqualTo("test"));
        }