Reputation: 137
Can anyone see where I have went wrong in the following example. I am writing tests to check for an if ID already exists. I have a 'UserService' that calls methods in a generic repository 'RentalsRepository' that makes the calls to the data for me. *Note the code works in the system, just not in my tests
Repo
public class RentalsRepository<T> : IRentalsRepository<T> where T : BaseClass
{
private readonly RentalsDBContext _Context;
private DbSet<T> entities;
string errorMessage = string.Empty;
public RentalsRepository(RentalsDBContext _Context)
{
this._Context = _Context;
entities = _Context.Set<T>();
}
public T Get(string Id)
{
return entities.SingleOrDefault(e => e.Id == Id);
} ...
UserService
public class UserService : IUserService {
private IRentalsRepository<UserAccount> _userRepository;
public UserService(IRentalsRepository<UserAccount> userRepository)
{
this._userRepository = userRepository;
}
public UserAccount GetUserFromId(string id)
{
UserAccount user = _userRepository.Get(id);
user.Email = Encryption.DecryptFromDatabase(user.Email);
return user;
}...
Test Class
[Fact]
public void UserService_GetByID()
{
var users = new List<UserAccount> {
new UserAccount { Id = "idstring1", Username = "username1"},
new UserAccount { Id = "idstring2", Username = "username2" },
new UserAccount { Id = "idstring3", Username = "username3" },
}.AsQueryable();
var mockSet = new Mock<DbSet<UserAccount>>();
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Provider).Returns(users.Provider);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Expression).Returns(users.Expression);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.ElementType).Returns(users.ElementType);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());
var mockContext = new Mock<RentalsDBContext>();
mockContext.Setup(m => m.Set<UserAccount>()).Returns(mockSet.Object);
var mockRepo = new Mock<IRentalsRepository<UserAccount>>();
mockRepo.Setup(m => m.Get(It.IsAny<string>())).Returns(mockSet.Object.FirstOrDefault());
var service = new UserService(mockRepo.Object);
UserAccount results = service.GetUserFromId("non-existant");
Assert.Equal("idstring1", results.Id);
}
When I debug I can see the value '"non-existant"' is being used in the method public UserAccount GetUserFromId(string id)
but it still somehow returns a user
Latest Attempt
[Fact]
public void UserService_GetUserByUsername()
{
byte[] b = Encryption.GetSalt();
var users = new List<UserAccount> {
new UserAccount { Id = "idstring2", Username = "username2" },
new UserAccount { Id = "idstring3", Username = "username3" },
}.AsQueryable();
var mockContext = new Mock<RentalsDBContext>();
var mockSet = new Mock<DbSet<UserAccount>>();
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Provider).Returns(users.Provider);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Expression).Returns(users.Expression);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.ElementType).Returns(users.ElementType);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());
mockContext.Setup(m => m.Set<UserAccount>()).Returns(mockSet.Object);
var mockRepo = new Mock<RentalsRepository<UserAccount>>(mockContext.Object);
var testClass = new UserService(mockRepo.Object);
UserAccount results = testClass.GetUserByUsername("username2");
Assert.Equal("username1", results.Username);
}
Upvotes: 0
Views: 98
Reputation: 7211
mockRepo.Setup(m => m.Get(It.IsAny<string>())).Returns(mockSet.Object.FirstOrDefault());
Is going to return the first record in your mock set ("idstring1") no matter what string you pass into Get()
. Assuming you get your mocked db context into your repository correctly, there is no reason to mock Get()
at all.
Having said that, if you're trying to test if an ID already exists, that's a function of the repository, not your service. All your service is doing is decrypting the email. You're testing both the repository and the service this way, which is not a unit test.
We're back to the question of what you are trying to test. If you want to test that the repository retrieves the correct user account, you would mock the db context and use the real repository class.
[Fact]
public void UserRepository_Get()
{
var users = new List<UserAccount> {
new UserAccount { Id = "idstring2", Username = "username2" },
new UserAccount { Id = "idstring3", Username = "username3" },
}.AsQueryable();
var mockSet = new Mock<DbSet<UserAccount>>();
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Provider).Returns(users.Provider);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Expression).Returns(users.Expression);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.ElementType).Returns(users.ElementType);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());
var mockContext = new Mock<RentalsDBContext>();
mockContext.Setup(m => m.Set<UserAccount>()).Returns(mockSet.Object);
var testClass = new RentalsRepository<userAccount>(mockContext.Object);
var results = testClass.Get("username2");
Assert.Equal("username2", results.Username);
}
If you want to test that the user service retrieves the user and decrypts the email, you would mock the repository (and it's Get
function) and use the real service class.
[Fact]
public void UserService_GetUserByUsername()
{
var userAccount = new UserAccount { Id = "idstring2", Username = "username2", Email = "" };
var mockRepo = new Mock<IRentalsRepository<UserAccount>>();
mockRepo.Setup(m => m.Get("idstring2").Returns(userAccount);
var testClass = new UserService(mockRepo.Object);
var results = testClass.GetUserByUsername("idstring2");
Assert.Equal("idstring2", results.Username);
Assert.AreEqual("???", results.Email);
}
If you want to test both the repository and service together, you certainly can, but it won't be a unit test since you're testing two things at once. In that case, you mock the db context and use real repository and service classes.
[Fact]
public void UserRepository_Get()
{
var users = new List<UserAccount> {
new UserAccount { Id = "idstring2", Username = "username2" },
new UserAccount { Id = "idstring3", Username = "username3" },
}.AsQueryable();
var mockSet = new Mock<DbSet<UserAccount>>();
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Provider).Returns(users.Provider);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Expression).Returns(users.Expression);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.ElementType).Returns(users.ElementType);
mockSet.As<IQueryable<UserAccount>>().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());
var mockContext = new Mock<RentalsDBContext>();
mockContext.Setup(m => m.Set<UserAccount>()).Returns(mockSet.Object);
var repository = new RentalsRepository<userAccount>(mockContext.Object);
var service = new UserService(repository);
var results = service.GetUserByUsername("username2");
Assert.Equal("username2", results.Username);
}
Upvotes: 2