Exemplo n.º 1
0
        public void TestThatExceptionIsThrownWhenMethodIsCalledMoreThanExpected()
        {
            var        stub      = new StubIPhoneBook().GetContactPhoneNumber((p1, p2) => 12345678, Times.Once);
            IPhoneBook phoneBook = stub;

            phoneBook.GetContactPhoneNumber("John", "Smith");
            phoneBook.GetContactPhoneNumber("John", "Smith");
        }
Exemplo n.º 2
0
        public void TestMethod_WithReturnType_WithParameters_DefaultBehavior_Loose()
        {
            var        stub      = new StubIPhoneBook(MockBehavior.Loose);
            IPhoneBook phoneBook = stub;

            Assert.AreEqual(0, phoneBook.GetContactPhoneNumber("John", "Smith"));
        }
Exemplo n.º 3
0
        public void TestThatExceptionIsThrownWhenStubIsNotSetup()
        {
            var        stub      = new StubIPhoneBook();
            IPhoneBook phoneBook = stub;

            phoneBook.GetContactPhoneNumber("John", "Smith");
        }
Exemplo n.º 4
0
        public void TestThatDefaultBehaviorIsUsedWhenStubIsNotSetup()
        {
            var        stub      = new StubIPhoneBook();
            IPhoneBook phoneBook = stub;
            var        result    = phoneBook.GetContactPhoneNumber("John", "Smith");

            Assert.AreEqual(0, result);
        }
Exemplo n.º 5
0
        public void TestThatLooseMockBehaviorsAreAlwaysOverwritten()
        {
            var stub = new StubIPhoneBook(MockBehavior.Loose)
                       .GetContactPhoneNumber((p1, p2) => 12345678);

            IPhoneBook phoneBook = stub;

            Assert.AreEqual(12345678, phoneBook.GetContactPhoneNumber("John", "Smith"));
        }
Exemplo n.º 6
0
        public void TestThatMethodStubCanBeOverwritten()
        {
            var stub = new StubIPhoneBook().GetContactPhoneNumber((p1, p2) => 12345678);

            stub.GetContactPhoneNumber((p1, p2) => 11122233, overwrite: true);

            IPhoneBook phoneBook = stub;

            Assert.AreEqual(11122233, phoneBook.GetContactPhoneNumber("John", "Smith"));
        }
Exemplo n.º 7
0
        private InvocationInfo GetContactPhoneNumber(InvocationInfo invocationInfo)
        {
            const int firstNameIndex = 0;
            const int lastNameIndex = 1;
            var result = _realInstance.GetContactPhoneNumber(
                (string)invocationInfo.Arguments[firstNameIndex].Value,
                (string)invocationInfo.Arguments[lastNameIndex].Value);

            invocationInfo.Result.Value = result;

            return invocationInfo;
        }
Exemplo n.º 8
0
        public void TestMethod_WithReturnType_WithParameters()
        {
            long   number    = 6041234567;
            string firstName = null;
            string lastName  = null;
            var    stub      = new StubIPhoneBook();

            stub.GetContactPhoneNumber((fn, ln) =>
            {
                firstName = fn;
                lastName  = ln;
                return(number);
            });
            IPhoneBook phoneBook    = stub;
            long       actualNumber = phoneBook.GetContactPhoneNumber("John", "Smith");

            Assert.AreEqual(number, actualNumber);
            Assert.AreEqual("John", firstName);
            Assert.AreEqual("Smith", lastName);
        }
Exemplo n.º 9
0
        public void TestCallSequence()
        {
            var stub = new StubIPhoneBook()
                       .GetContactPhoneNumber((p1, p2) => 12345678, Times.Once)     // first call
                       .GetContactPhoneNumber((p1, p2) => 11122233, Times.Twice)    // next two calls
                       .GetContactPhoneNumber((p1, p2) => 22233556, Times.Forever); // rest of the calls

            IPhoneBook phoneBook = stub;

            Assert.AreEqual(12345678, phoneBook.GetContactPhoneNumber("John", "Smith"));
            Assert.AreEqual(11122233, phoneBook.GetContactPhoneNumber("John", "Smith"));
            Assert.AreEqual(11122233, phoneBook.GetContactPhoneNumber("John", "Smith"));
            Assert.AreEqual(22233556, phoneBook.GetContactPhoneNumber("John", "Smith"));
            Assert.AreEqual(22233556, phoneBook.GetContactPhoneNumber("John", "Smith"));
            Assert.AreEqual(22233556, phoneBook.GetContactPhoneNumber("John", "Smith"));
        }