コード例 #1
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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>());
        }
コード例 #2
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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));
        }
コード例 #3
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        public void YouCanSetAPropertyWithASequenceOfReturnValues()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Set the property.
            stub.Property.Returns("foo", "bar");

            // Check the property.
            stub.Property.Should(Be.EqualTo("foo"));
            stub.Property.Should(Be.EqualTo("bar"));
        }
コード例 #4
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>()));
        }
コード例 #5
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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());
        }
コード例 #6
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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();
        }
コード例 #7
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));
        }
コード例 #8
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()));
        }
コード例 #9
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));
        }
コード例 #10
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));
        }
コード例 #11
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"));
        }
コード例 #12
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        public void YouCanUseAssertWasCalledWithPropertiesOnAStub()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Set the property.
            stub.Property = "foo";

            // Get the property.
            stub.Property.Should(Be.EqualTo("foo"));

            // Check that the property was set and get.
            stub.Received().Property = "foo";
            stub.Received().Property.Should(Be.Null);
        }
コード例 #13
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));
        }
コード例 #14
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        public void YouCannotUseAssertWasCalledWithPropertiesOnAStub()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // But why would you need to?  You can just get the value
            // directly from the property.
            stub.Property = "foo";

            // Don't do this
            // stub.AssertWasCalled(s => s.Property);

            // Just do this.
            stub.Property.Should(Be.EqualTo("foo"));
        }
コード例 #15
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        public void YouCanTellEventsOnAStubToFire()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            bool eventWasHandled = false;

            // Attach an event handler.
            stub.SomeEvent += (args, e) => eventWasHandled = true;

            // Raise the event.
            stub.Raise(s => s.SomeEvent += null, this, EventArgs.Empty);

            // Check that event was handled.
            eventWasHandled.Should(Be.True);
        }
コード例 #16
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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>());
        }
コード例 #17
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        public void HandlingOutParametersInStubs()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

            // Here's how you stub an "out" parameter.
            int outi;

            stub.WhenForAnyArgs(s => s.MethodWithOutParameter(out outi)).Do(x => x[0] = 10);

            // Call method with out parameters.
            int i;

            stub.MethodWithOutParameter(out i);
            i.Should(Be.EqualTo(10));
        }
コード例 #18
0
        public void YouCanUseAssertWasCalledWithPropertiesOnAStub()
        {
            // Create a stub.
            ISampleClass        stub      = CreateStub();
            Mock <ISampleClass> stubClass = Mock.Get(stub);

            // Set the property.
            stub.Property = "foo";

            // Get the property.
            stub.Property.Should(Be.EqualTo("foo"));

            // Check that the property was set and get.
            stubClass.VerifySet(s => s.Property = "foo");
            stubClass.VerifyGet(s => s.Property);
        }
コード例 #19
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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"));
        }
コード例 #20
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));
        }
コード例 #21
0
        public void HandlingOutParametersInStubs()
        {
            // Create a stub.
            ISampleClass        stub      = CreateStub();
            Mock <ISampleClass> stubClass = Mock.Get(stub);

            // Here's how you stub an "out" parameter.
            var outi = 10;

            stubClass.Setup(s => s.MethodWithOutParameter(out outi));

            // Call method with out parameters.
            int i;

            stub.MethodWithOutParameter(out i);
            i.Should(Be.EqualTo(10));
        }
コード例 #22
0
        public void HandlingRefParametersInStubs()
        {
            // Create a stub.
            ISampleClass stub = CreateStub();

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

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

            // If I call the method with any other input argument, it will
            // not change the parameter to the value you specified.
            param = "some other value";
            stub.MethodWithRefParameter(ref param);
            param.Should(Be.EqualTo("some other value"));
        }
コード例 #23
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));
        }
コード例 #24
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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));
        }
コード例 #25
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"));
        }
コード例 #26
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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");
        }
コード例 #27
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);
        }
コード例 #28
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));
        }
コード例 #29
0
ファイル: StubTests.cs プロジェクト: virgiliofornazin/NDepth
        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());
        }
コード例 #30
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());
        }
コード例 #31
0
 public HomeController(
     ISampleClass sampleClass)
 {
     _sampleClass = sampleClass;
 }