In my three layer application i faced with problem, that consists in substitute database in unit tests. I have a class that implements bussines logic for entity of User:
public class GuestService : IGuestService
{
private readonly IUnitOfWork Database = EFUnitOfWork.GetDatabase();
private readonly IMapper mapper =
new MapperConfiguration(cfg => cfg.CreateMap<Product, ProductDTO>()).CreateMapper();
public bool RegistrationValidation(string login, string password, out string resultString)
{
if (!Regex.Match(login, @"^(?=.{6,255}$)[a-zA-Z0-9]*$").Success)
{
resultString = "Login might be more than 6 characters, and consist only letters and digits";
return false;
}
if (!Regex.Match(password, @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15}$").Success)
{
resultString = "Password needs to be more than 8 characters and doesn't consists special symbols like: . / '...";
return false;
}
resultString = string.Empty;
return true;
}
public IEnumerable<ProductDTO> GetAllProducts()
{
var allProducts = Database.Products.GetAll();
return mapper.Map<IEnumerable<ProductDTO>>(allProducts);
} ...
And other method that gives different functionality. So i want to test this class with NUnit using moq extension
GuestService service = new GuestService();
public IEnumerable<User> GetUsers()
{
return new List<User>
{
new User("login","password"),
new User("login1", "password1"),
new User("login2", "password2")
};
}
[Test]
public void Test1()
{
var mock = new Mock<IRepository<User>>();
mock.Setup(repo => repo.GetAll()).Returns(GetUsers());
service.LogIn("login", "password");
}
But i can't substitute database because database object are hidden in GuestService
class. So how do i need to solve this problem? I will be grateful for the answer.
Aucun commentaire:
Enregistrer un commentaire