public void GetProfileByUserId_AnyId_CallsQueryMethodOfRepository() { var settingsMock = new Mock<ISettings>(); var componentsMock = new Mock<IComponents>(); var repositoryMock = new Mock<IRepository>(); var uowMock = new Mock<IUnitOfWork>(); var serviceLocatorMock = new Mock<IServiceLocator>(); serviceLocatorMock.Setup(x => x.GetInstance<IRepository>()) .Returns(repositoryMock.Object); serviceLocatorMock.Setup(x => x.GetInstance<IUnitOfWork>()) .Returns(uowMock.Object); ServiceLocator.SetLocatorProvider(() => serviceLocatorMock.Object); // Arrange string token = Guid.NewGuid().ToString(); Guid id = Guid.NewGuid(); Guid profileId= Guid.NewGuid(); User user = new User() { Id = id, CreatedDate = DateTime.Now }; Profile profile = new Profile() { Id =profileId, User = user }; Profile[] profiles = new Profile[] { profile }; repositoryMock.Setup(r => r.Query<Profile>()).Returns(profiles.AsQueryable()); repositoryMock.Setup(r => r.CreateUnitOfWork()).Returns(uowMock.Object); // Act var userService = new UserService(uowMock.Object,repositoryMock.Object, settingsMock.Object, componentsMock.Object); ProfileDto actuaProfile = userService.GetProfileByUserId(id); // Assert repositoryMock.Verify(repo => repo.Query<Profile>()); Assert.AreEqual(actuaProfile.Id, profile.Id); }