コード例 #1
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        public void YouCanTellTheStubToThrowAnExceptionWhenAMethodIsCalled()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Calling the void method will throw exception.
            stub.Stub(s => s.VoidMethod()).Throw(new InvalidOperationException());
            // Calling the method with "foo" as the parameter will throw exception.
            stub.Stub(s => s.MethodThatReturnsInteger("foo")).Throw(new InvalidOperationException());

            // Call methods that throw exception.
            Assert.Throws <InvalidOperationException>(stub.VoidMethod);
            Assert.Throws <InvalidOperationException>(() => stub.MethodThatReturnsInteger("foo"));
        }
コード例 #2
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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));
        }
コード例 #3
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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));
        }
コード例 #4
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        public void HandlingOutParametersInStubs()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Here's how you stub an "out" parameter.  The "Dummy" part is
            // just to satisfy the compiler.
            stub.Stub(s => s.MethodWithOutParameter(out Arg <int> .Out(10).Dummy));

            // Call method with out parameters.
            int i;

            stub.MethodWithOutParameter(out i);
            i.Should(Be.EqualTo(10));
        }
コード例 #5
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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));
        }
コード例 #6
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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));
        }
コード例 #7
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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));
        }
コード例 #8
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        public void HandlingRefParametersInStubs()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Here's how you stub an "ref" parameter.  The "Dummy" part is
            // just to satisfy the compiler.  (Note: Is.Equal() is part of
            // the Rhino.Mocks.Contraints namespace, there is also an
            // Is.EqualTo() in NUnit... you want the Rhino Mocks one.)
            stub.Stub(s => s.MethodWithRefParameter(ref Arg <string> .Ref(Rhino.Mocks.Constraints.Is.Equal("input"), "output").Dummy));

            // If you call the method with the specified input argument, it will
            // change the parameter to the value you specified.
            string param = "input";

            stub.MethodWithRefParameter(ref param);
            param.Should(Be.EqualTo("output"));

            // If I call the method with any other input argument, it won't
            // change the value.
            param = "some other value";
            stub.MethodWithRefParameter(ref param);
            param.Should(Be.EqualTo("some other value"));
        }