Пример #1
0
        public void getFriends_FriendsFound()
        {
            List <User> users = new List <User>();

            users.PopulateUsers(3);

            List <User> mockedUsers = new List <User>();

            foreach (User user in users)
            {
                mockedUsers.Add(MockingHelper.CreateUserCopy(user));
            }

            _connectionRepoMock.Setup(f => f.getFriends(It.IsAny <string>())).Returns(users);

            var result = _connectionServiceMock.Object.getFriends(users.First().UserName);

            _connectionRepoMock.Verify(f => f.getFriends(It.Is <string>(k => k == users.First().UserName)), Times.Once);

            Assert.AreEqual(mockedUsers.Count, result.Count);

            for (int i = 0; i < result.Count; i++)
            {
                MockingHelper.CheckAssertsForUser(mockedUsers.Skip(i).First(), result.Skip(i).First());
            }
        }
        public void getUserByUsername_UserFound()
        {
            User mockedUser = _users.PopulateUsers(1).FirstOrDefault();

            User expected = MockingHelper.CreateUserCopy(mockedUser);

            _userRepositoryMock.Setup(f => f.getUserByUsername(It.IsAny <string>())).Returns(mockedUser);

            var result = _userServiceMock.Object.getUserByUsername(mockedUser.UserName);

            _userRepositoryMock.Verify(f => f.getUserByUsername(It.Is <string>(v => v == mockedUser.UserName)), Times.Once);

            MockingHelper.CheckAssertsForUser(expected, result);
        }
        public void getUserById_UserFound()
        {
            User mockedUser = _users.PopulateUsers(1).FirstOrDefault();

            User expected = MockingHelper.CreateUserCopy(mockedUser);

            //We use AAA syntax in each unit test in this project

            //Arrange
            _userRepositoryMock.Setup(f => f.getUserById(It.IsAny <string>())).Returns(mockedUser);

            //Act
            var result = _userServiceMock.Object.getUserById(mockedUser.Id);

            //Assert
            _userRepositoryMock.Verify(f => f.getUserById(It.Is <string>(v => v == mockedUser.Id)), Times.Once);

            MockingHelper.CheckAssertsForUser(expected, result);
        }
        public void getAll_UsersFound()
        {
            _users.PopulateUsers(5);

            foreach (User user in _users)
            {
                expectedUsers.Add(MockingHelper.CreateUserCopy(user));
            }

            _userRepositoryMock.Setup(f => f.getAll()).Returns(_users);

            var result = _userServiceMock.Object.getAll();

            _userRepositoryMock.Verify(f => f.getAll(), Times.Once);

            Assert.AreEqual(expectedUsers.Count, result.Count);

            for (int i = 0; i < result.Count; i++)
            {
                MockingHelper.CheckAssertsForUser(expectedUsers.Skip(i).First(), result.Skip(i).First());
            }
        }