public void TestPersist()
        {
            Mockery mocks = new Mockery();

            //Crate mocks
            IUserGateway mockGateway = mocks.NewMock<IUserGateway>();
            IUserValidator mockValidator = mocks.NewMock<IUserValidator>();

            //Create user
            User user = new User();

            //Expectations
            using(mocks.Ordered)
            {
                Expect.Once.On(mockValidator).Method("Validate").With(user).Will(Return.Value(true));
                Expect.Once.On(mockGateway).Method("Persist").With(user).Will(Return.Value(true));
            }

            //Assign gateway
            user.Gateway = mockGateway;

            //Test method
            Assert.AreEqual(true, user.Persist(mockValidator));

            mocks.VerifyAllExpectationsHaveBeenMet();
        }
        public void TestPersistWithNaturalMocking()
        {
            MockManager.Init();

            //Create User
            User user = new User();

            // Natural mocking
            using(RecordExpectations recorder = new RecordExpectations())
            {
                IUserGateway mockGateway = new UserGateway();
                IUserValidator mockValidator = new UserValidator();

                //Expectations
                recorder.ExpectAndReturn(mockValidator.Validate(user), true);
                recorder.ExpectAndReturn(mockGateway.Persist(user), true);
            }

            //Create instances
            IUserGateway gateway = new UserGateway();
            IUserValidator validator = new UserValidator();

            //Assign gateway
            user.Gateway = gateway;

            //Test method
            Assert.AreEqual(true, user.Persist(validator));

            MockManager.Verify();
        }
        public void TestPersist()
        {
            MockFactory factory = new MockFactory();

            //Create mocks
            Mock<IUserGateway> mockGateway = factory.CreateMock<IUserGateway>();
            Mock<IUserValidator> mockValidator = factory.CreateMock<IUserValidator>();

            //Create user
            User user = new User();

            //Expectations
            using(factory.Ordered)
            {
                mockValidator.Expects.One.MethodWith(m => m.Validate(user)).WillReturn(true);
                mockGateway.Expects.One.MethodWith(m => m.Persist(user)).WillReturn(true);
            }

            //Assign gateway
            user.Gateway = mockGateway.MockObject;

            //Test method
            Assert.AreEqual(true, user.Persist(mockValidator.MockObject));

            factory.VerifyAllExpectationsHaveBeenMet();
        }
        public void TestPersist_ValidatorClass()
        {
            // Arrange
            var gateway = Substitute.For<UserGateway>();
            var validator = Substitute.For<UserValidator>();
            User user = new User();
            user.Gateway = gateway;
            validator.Validate(user).Returns(true);
            gateway.Persist(user).Returns(true);

            // Act
            bool bRet = user.Persist(validator);

            // Assert
            Assert.AreEqual(true, bRet);
            validator.Received().Validate(user);
            gateway.Received().Persist(user);
        }
        public void TestPersistWithAAA()
        {
            //Arrange
            //Create mocks
            UserGateway gateway = Isolate.Fake.Instance<UserGateway>();
            IUserValidator validator = Isolate.Fake.Instance<IUserValidator>();
            //Create user
            User user = new User(gateway);
            //Expectations
            Isolate.WhenCalled(() => validator.Validate(user)).WillReturn(true);
            Isolate.WhenCalled(() => gateway.Persist(user)).WillReturn(true);

            //Act
            bool bPersist = user.Persist(validator);

            //Assert
            Assert.AreEqual(true, bPersist);
        }
        public void TestPersist()
        {
            //Gateway
            DynamicMock mockGateway = new DynamicMock(typeof(IUserGateway));
            IUserGateway gateway = (IUserGateway) mockGateway.MockInstance;

            //Validator
            DynamicMock mockValidator = new DynamicMock(typeof(IUserValidator));
            IUserValidator validator = (IUserValidator)mockValidator.MockInstance;

            //User
            User user = new User(gateway);

            //Expectations
            mockValidator.ExpectAndReturn("Validate", true, user);
            mockGateway.ExpectAndReturn("Persist", true, user);

            Assert.AreEqual(true, user.Persist(validator));
            mockValidator.Verify();
            mockGateway.Verify();
        }
        public void TestPersist()
        {
            var gateway = new Mock<IUserGateway>();
            var validator = new Mock<IUserValidator>();

            //Create user
            User user = new User();

            //Excpectations
            validator.Setup(x => x.Validate(user)).Returns(true).AtMostOnce();
            gateway.Setup(x => x.Persist(user)).Returns(true).AtMostOnce();

            //Assign gateway
            user.Gateway = gateway.Object;

            //Test method
            Assert.AreEqual(true, user.Persist(validator.Object));

            validator.VerifyAll();
            gateway.VerifyAll();
        }
        public void TestPersistWithFactory()
        {
            MockFactory factory = new MockFactory(MockBehavior.Strict);

            //Classes work, methods have to be virtual -> not nice
            var mockGateway = factory.Create<UserGateway>();
            var mockValidator = factory.Create<IUserValidator>();

            User user = new User();

            //Excpectations
            mockValidator.Setup(x => x.Validate(user)).Returns(true).AtMostOnce();
            mockGateway.Setup(x => x.Persist(user)).Returns(true).AtMostOnce();

            //Assign gateway
            user.Gateway = mockGateway.Object;

            //Test method
            Assert.AreEqual(true, user.Persist(mockValidator.Object));

            factory.VerifyAll();
        }
        public void TestPersist()
        {
            //Create mocks
            var expectationScope = new ExpectationScope();
            IUserGateway mockGateway = Mock.Interface<IUserGateway>(expectationScope);
            IUserValidator mockValidator = Mock.Interface<IUserValidator>(expectationScope);

            //Create user
            User user = new User();

            //Expectations
            Expect.Once.MethodCall(() => mockValidator.Validate(user)).Returns(true);
            Expect.Once.MethodCall(() => mockGateway.Persist(user)).Returns(true);

            //Assign gateway
            user.Gateway = mockGateway;

            //Test method
            Assert.AreEqual(true, user.Persist(mockValidator));

            AssertExpectations.IsMetFor(expectationScope);
        }
        public void TestPersistWithReflectiveMocking()
        {
            MockManager.Init();

            //Create mocks
            Mock mockGateway = MockManager.Mock<UserGateway>();
            Mock mockValidator = MockManager.Mock<UserValidator>();

            //Create user
            UserGateway gateway = new UserGateway();
            IUserValidator validator = new UserValidator();
            User user = new User(gateway);

            //Expectations
            mockValidator.ExpectAndReturn("Validate", true).Args(user);
            mockGateway.ExpectAndReturn("Persist", true).Args(user);

            //Test method
            Assert.AreEqual(true, user.Persist(validator));

            MockManager.Verify();
        }