I'm new to unit testing.Can anyone explain me about how to done unit testing without hitting Database.
And also i want to know that, Is dependency injection essential for unit testing?
If yes, explain me with a sample code. It will be helpful for me.
I had already created unit testing for login, which hits Database. But i want to test the same case for the login, without hitting database.
In this, i used Microsoft.VisualStudio.TestTools.UnitTesting.
Here is my code,
[TestMethod]
public void _01_LoginUser_01_Valid()
{
BLUser.User.UserDTO user = new BLUser.User.UserDTO();
user.UserName = "mohan";
user.UserPassword = "abc";
BLUser.Model.Fs_User result = BLUser.User.LoginUser(user);
Assert.AreEqual("mohan", result.UserName);
}
And my business logic is,
public static Fs_User LoginUser(UserDTO userDTO)
{
try
{
var context = new UserDBEntities();
{
var LoginUser = context.Fs_User.Where(u => u.UserName == userDTO.UserName && u.UserPassword == userDTO.UserPassword).SingleOrDefault();
if (LoginUser == null)
ValidationError.LoginException((int)ExceptionCodes.InvalidPassword);
return LoginUser;
}
}
catch (Exception ex)
{
throw ex;
}
}
See my answer here: Solid Principle examples anywhere?
If you look at the idea of depending on an interface for your data access, you'll see how you could supply a 'fake' implementation of that interface for testing that would not depend on a database.
Based on your example, you need to make something like the following changes:
public class LoginThing
{
private readonly IAmSomeContext context;
public LoginThing(IAmSomeContext context)
{
this.context = context;
}
public Fs_User LoginUser(UserDTO userDTO)
{
try
{
var LoginUser =
this.context.Fs_User
.SingleOrDefault(
u =>
u.UserName == userDTO.UserName
&& u.UserPassword == userDTO.UserPassword);
if (LoginUser == null)
ValidationError.LoginException((int)ExceptionCodes.InvalidPassword);
return LoginUser;
}
catch (Exception ex)
{
throw ex;
}
}
}
And then your test can become something like:
[TestMethod]
public void _01_LoginUser_01_Valid()
{
BLUser.User.UserDTO user = new BLUser.User.UserDTO();
user.UserName = "mohan";
user.UserPassword = "abc";
var fakeContext = CreateFakeContextWith(user);
var thingUnderTest = new LoginThing(fakeContext);
BLUser.Model.Fs_User result = thingUnderTest.LoginUser(user);
Assert.AreEqual("mohan", result.UserName);
}
Creating the fake context would look something like this, if you used NSubstitute:
private IAmSomeContext CreateFakeContextWith(BLUser.User.UserDTO user)
{
var fakeContext = Substitute.For<IFakeContext>();
fakeContext.Fs_User.Returns(new List(new[] {user}));
return fakeContext;
}
The syntax might not be exact, but you get the point...
Related
I'm trying to do some unittesting on a method that is in a FreshMVVM view model (so no interface).
I want to parse two properties with values as well.
I think I found the way to parse the properties. But I get the following exception while running the tests :
Non-overridable members (here: Search ViewModel.ExecuteSearch Command) may not be used in setup / verification expressions.
The method is set public and so are the properties. I can not change them to virtual because then I get an error in my method.
here is my code:
Viewmodel:
public async void ExecuteSearchCommand()
{
ProductionOrders.Clear();
ObservableCollection<ProductionOrder> allProductionorders = await GetDetailedProductionOrders();
if (SelectedSearch == null || Input== null) {
await Application.Current.MainPage.DisplayAlert("woeps", "please make your selection", "OK");
}
else
{
if (SelectedSearch == "Material")
{
foreach (var productionOrder in allProductionorders)
{
if (productionOrder.MaterialNumber == Input)
{
ProductionOrders.Add(productionOrder);
}
}
}
else
{
foreach (var productionOrder in allProductionorders)
{
if (productionOrder.OrderNumber == int.Parse(Input))
{
ProductionOrders.Add(productionOrder);
}
}
}
if (productionOrders.Count == 0)
{
await Application.Current.MainPage.DisplayAlert("woeps", "No data found for this selection", "OK");
}
}
unit test:
[Fact]
public void ExecuteSearchCommand_WitCorrectData_ListProductionOrders()
{
//Arrange
var testMaterial=testMaterials[0];
var testProductionOrder = testProductionOrders[0];
var mockVm = new Mock<SearchViewModel>();
//act
mockVm.Setup(vm => vm.ExecuteSearchCommand()).Equals(testProductionOrder);
mockVm.SetupProperty(se => se.SelectedSearch,"Production Order") ;
mockVm.SetupProperty(ip => ip.Input, "100001");
Assert.NotNull(mockVm);
}
I also tried this:
[Fact]
public void ExecuteSearchCommand_WitCorrectData_ListProductionOrders()
{
//Arrange
var testMaterial=testMaterials[0];
var testProductionOrder = testProductionOrders[0];
var mockVm = new SearchViewModel { SelectedSearch = "Production Order", Input="100001", ProductionOrders=new ObservableCollection<ProductionOrder>() };
mockVm.ExecuteSearchCommand();
//act
Assert.NotNull(mockVm);
}
But then I get an error in the GetDetailedProductionorders method used in the executesearchcommand()
I don't get this error when running the program (not the unit test)
Could someone give me a hint in the right direction?
Thx!
Sarah
From the second unit test you tried, when you create instance of SearchViewModel, there is no initialize of _productionOrderService.
if _productionOrderService is created in SearchViewModel it might not be initialized due to lack of their dependencies.
you have to provide _productionOrderService to the SearchViewModel by
make it public and set it when create SearchViewModel
make it private and pass it through constructor when create SearchViewModel
then you can mock _productionOrderService and setup GetListAllAsync() in unit test
I am using InMemoryDatabase for writing testcases, but when I want to test relational database operations(Insert/Update) I could not able to use InMemoryDatabase.
If I am using InMemoryDatabase I am getting an error like
System.InvalidOperationException: 'Relational-specific methods can only be used when the context is using a relational database provider.'
Is there any way to test relational database operations(Insert/Update) without touching the development or production database?
This is the repository class which uses the relational db operations
public bool UpdateOrInsert(Model model, Details details = null)
{
//_context is SqlDbContext
using (var transaction = _context.Database.BeginTransaction())
{
try
{
if(details.Id > 0)
{
//Update details;
_context.SaveChanges();
}
else
{
//Insert details;
_context.SaveChanges();
}
if(model.Id > 0)
{
//Update model;
_context.SaveChanges();
}
else
{
//Insert model;
_context.SaveChanges();
}
transaction.Commit();
return true;
}
catch(Exception ex)
{
transaction.Rollback();
throw;
}
}
}
How can we write unit test for this method without touching the real database? Is there any way?
The testcase I used to test this method throws the above error.
[Fact]
public void CheckTest()
{
DbContextOptions<SqlDbContext> _options;
_options = new DbContextOptionsBuilder<SqlDbContext>()
.UseInMemoryDatabase(databaseName: "DbName1")
.Options;
using (var context = new SqlDbContext(_options))
{
context.Model.Add(_model);//_model= sample model object
context.Details.Add(_details);//_details = sample details object
context.SaveChanges();
var cls = new ClassName();
var result = cls.UpdateOrInsert(_model,_details);
Assert.Assert.IsType<Bool>(result);
}
}
If I use the following way to check, it will be possible to update the original database.
[Fact]
public void CheckTest()
{
var connectionString = #"Data Source = *****; Initial Catalog = ******; uid = ****; Pwd = ******";
DbContextOptions<SqlDbContext> _sqlOptions = new DbContextOptionsBuilder<SqlDbContext>()
.UseSqlServer(connectionString)
.ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning))
.Options;
using (var context = new SqlDbContext(_sqlOptions))
{
var cls = new ClassName();
var result = cls.UpdateOrInsert(_model,_details);
Assert.Assert.IsType<Bool>(result);
}
}
Do we have any other way to write test case for the method - UpdateOrInsert without touching the relational database?
Is it possible to test via InMemmoryDatabase?
Please help
I'm quite new to unit testing and I need an hand to understand if I'm doing things in the correct way. My major problem is regarding the DB testing... Here's my code then I'll expose my perplexities
Consider this class that's an item of a pipeline I've to perform
public class RetrieveApplicationUsernamePipelineStep : IPipelineStep
{
public const string RetrieveApplicationUsernameKey = "RetrieveApplicationUsername";
private readonly IRetrieveApplicationUserRepository repository;
public int Order => 3;
public string Name => RetrieveApplicationUsernameKey;
public RetrieveApplicationUsernamePipelineStep(IRetrieveApplicationUserRepository repository)
{
this.repository = repository;
}
public async Task<IDictionary<string, object>> Action(IDictionary<string, object> context)
{
string res = await repository.GetApplicationUser(context);
context[Resources.ApplicationUser] = res;
return context;
}
}
I wrote the following tests
[TestFixture]
public class RetrieveApplicationUsernamePipelineStepTests
{
private IRetrieveApplicationUserRepository retrieveApplicationUserRepository;
[OneTimeSetUp]
public void Start()
{
var configuration = new ConfigurationFromConfigFile();
retrieveApplicationUserRepository = new RetrieveApplicationUserRepository(configuration);
}
[Test]
public async Task ActionSuccessfullyCompleted()
{
var context = new Dictionary<string, object>();
var repository = Substitute.For<IRetrieveApplicationUserRepository>();
repository.GetApplicationUser(context).Returns("user1");
var pipeline = new RetrieveApplicationUsernamePipelineStep(repository);
var res = await pipeline.Action(context);
Assert.IsNotNull(res[Resources.ApplicationUser]);
Assert.IsNotEmpty((string)res[Resources.ApplicationUser]);
}
[Test]
public void ActionFailingCompleted()
{
var context = new Dictionary<string, object>();
var repository = Substitute.For<IRetrieveApplicationUserRepository>();
repository.GetApplicationUser(context).Throws(new UserMappingNotFoundException());
var pipeline = new RetrieveApplicationUsernamePipelineStep(repository);
Assert.ThrowsAsync<UserMappingNotFoundException>(async () => await pipeline.Action(context));
}
[Test]
public void NameTest()
{
var pipeline = new RetrieveApplicationUsernamePipelineStep(retrieveApplicationUserRepository);
Assert.IsTrue(pipeline.Name == RetrieveApplicationUsernamePipelineStep.RetrieveApplicationUsernameKey);
}
[Test]
public void OrderTest()
{
var pipeline = new RetrieveApplicationUsernamePipelineStep(retrieveApplicationUserRepository);
Assert.IsTrue(pipeline.Order == 3);
}
}
And those test works fine since for ActionSuccessfullyCompleted and ActionFailingCompleted I substitute the IRetrieveApplicationUserRepository's result with my expected one.
The real implementation of ther repository is
public class RetrieveApplicationUserRepository : IRetrieveApplicationUserRepository
{
#region Variables
private readonly IConfiguration configuration;
#endregion
#region Ctor
public RetrieveApplicationUserRepository(IConfiguration configuration)
{
this.configuration = configuration;
}
#endregion
#region IRetrieveApplicationUserRepository
public async Task<string> GetApplicationUser(IDictionary<string, object> context)
{
if (configuration.AppSettings[Resources.ApplicationUserFromDomainUserKey] == null)
throw new KeyNotFoundException(Resources.ApplicationUserFromDomainUserKey);
if (string.IsNullOrEmpty(configuration.ConnectionString))
throw new NullReferenceException();
string storedProcedure = configuration.AppSettings.Get(Resources.ApplicationUserFromDomainUserKey);
string result;
using (var sqlConnection = new SqlConnection(configuration.ConnectionString))
{
using (var sqlCommand = new SqlCommand(storedProcedure, sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("#DOMAINUSER", context[Resources.DomainUser]);
sqlCommand.Parameters.AddWithValue("#DOMAIN", context[Resources.DomainName]);
sqlCommand.Parameters.AddWithValue("#APPID", context[Resources.ApplicationId]);
sqlConnection.Open();
result = (string)await sqlCommand.ExecuteScalarAsync();
}
}
if (result == null)
throw new UserMappingNotFoundException();
return result;
}
#endregion
}
Here're the questions :
Are the test I wrote correct?
I've seen using Resharper's Code Coverage that it wants me to test-cover the properties...is there a way I can avoid this? is this test meaningful?
What's your approach when you've to unit test component that're related to DB? Have you got a real-db that's used for test? Consider that the real DB is about 10Gb so I don't want to have a copy as mdf (condider I can have this) just to test a small portion of the DB
Talking with my colleagues they told me to use test just for TDD while I wish to use them to avoid regressions
Going back to the DB question, I don't want to have a test where I write if username is "John" and maybe tomorrow John user won't be present in the DB anymore,so that the test I expect to pass wont' pass anoymore
The usual approach is to isolate the database side with an abstraction, so you can provide a test dummy (mock, fake, etc) of that abstraction. Only test an actual database when you do the integration testing.
For the tests of database stored procedures, you may well want a different test harness, creating a new test database in memory (equivalently, in a RAM-backed filesystem). You only need to populate enough data for the individual test (we're doing functional testing here, not performance testing), and you may be able to retain table structure across tests with judicious use of rollback.
I have done this, but it's some time ago, so I'll refrain from giving examples that may be no longer state-of-the-art (even if the code does still exist, and if I could find it).
I am trying to test the AddCategory of the following CategoryService.
My problem is that I am having a hard time understanding what to mock/fake.
My attempt at the test is at the bottom.
I am using MOQ, xUnit and FluentAssertions.
I am using FluentValidation for the validators.
Category Service
public class CategoryService : ValidatingServiceBase, ICategoryService
{
private readonly IUnitOfWork unitOfWork;
private readonly IRepository<Category> categoryRepository;
private readonly IRepository<SubCategory> subCategoryRepository;
private readonly IValidationService validationService;
public CategoryService(
IUnitOfWork unitOfWork,
IRepository<Category> categoryRepository,
IRepository<SubCategory> subCategoryRepository,
IValidationService validationService)
: base(validationService)
{
this.unitOfWork = unitOfWork;
this.categoryRepository = categoryRepository;
this.subCategoryRepository = subCategoryRepository;
this.validationService = validationService;
}
public bool AddCategory(Category category)
{
var validationResult = validationService.Validate(category);
if (!validationResult.IsValid)
{
return false;
}
else
{
categoryRepository.Add(category);
return true;
}
}
public bool DoesCategoryExist(string categoryName)
{
return categoryRepository.Query().SingleOrDefault(x => x.Name == categoryName) != null;
}
}
Validation Service
public class ValidationService : ServiceBase, IValidationService
{
private readonly IValidatorFactory validatorFactory;
public ValidationService(IValidatorFactory validatorFactory)
{
Enforce.ArgumentNotNull(validatorFactory, "validatorFactory");
this.validatorFactory = validatorFactory;
}
public ValidationResult Validate<TEntity>(TEntity entity) where TEntity : class
{
var validator = validatorFactory.GetValidator<TEntity>();
return validator.Validate(entity);
}
}
Validator Factory
public class ValidatorFactory : IValidatorFactory
{
public IValidator GetValidator(Type type)
{
Enforce.ArgumentNotNull(type, "type");
return DependencyResolver.Current.GetService(typeof(IValidator<>).MakeGenericType(type)) as IValidator;
}
public IValidator<T> GetValidator<T>()
{
return DependencyResolver.Current.GetService<IValidator<T>>();
}
}
Category Validator
public class CategoryValidator : AbstractValidator<Category>
{
public CategoryValidator(ICategoryService service)
{
RuleFor(x => x.Name)
.NotEmpty()
.Must((category, name) =>
{
return service.DoesCategoryExist(name);
});
}
}
Unit Test Attempt
[Fact]
public void AddCategory_Should_ReturnTrue()
{
var category = new Category() { Name = "Cat1" };
var unitOfWork = new Mock<IUnitOfWork>();
var categoryRepo = new Mock<IRepository<Category>>();
var subCategoryRepo = new Mock<IRepository<SubCategory>>();
var mockCategoryService = new Mock<ICategoryService>();
var categoryValidator = new CategoryValidator(mockCategoryService.Object);
var validatorFactory = new Mock<IValidatorFactory>();
validatorFactory.Setup(x => x.GetValidator<CategoryValidator>()).Returns(categoryValidator as IValidator<CategoryValidator>);
var validationService = new ValidationService(validatorFactory.Object);
var categoryService = new CategoryService(
unitOfWork.Object,
categoryRepo.Object,
subCategoryRepo.Object,
validationService);
categoryService.AddCategory(category);
}
Well for the AddCategory method, I think you really only need two mocks, one for the ValidationService, and one for the CategoryRepository, because the other dependencies aren't exercised in that function and therefore are irrelevant
(the story might be different of course if your ctor throws on null arguments but in this case I think you are OK - albeit you might consider adding these checks in the future :)
Anyway, being pedantic, I'd nearly be inclined to write two (or more - maybe one for null input to verify it throws or returns false or whatever) "unit" tests for this function;
One to verify that given an invalid category, the function returns false,
One to verify that given a valid category, the function calls Add on the CategoryRepository dependency.
So it would look like this (sorry, this is using MSTest syntax as I'm not familiar with xUnit but it's the same idea). Also have not tested below for typos, etc :)
public void AddCategory_InvalidCategory_ShouldReturnFalse()
{
//Arrange
var mockValidator = new Mock<IValidator>();
//no matter what we pass to the validator, it will return false
mockValidator.Setup(v=>v.Validate(It.IsAny<Category>()).Returns(false);
var sut= new CategoryService(null,null,null,mockValidator.Object);
bool expected = false;
//ACT
bool actual = sut.AddCategory(new Category());
//ASSERT
Assert.AreEqual(expected,actual,"Validator didn't return false as expected");
}
public void AddCategory_ValidCategory_ShouldCallRepositoryAdd()
{
//Arrange
var mockValidator = new Mock<IValidator>();
//no matter what we pass to the validator, it will return true
mockValidator.Setup(v=>v.Validate(It.IsAny<Category>()).Returns(true);
var mockRepo = new Mock<IRepository<SubCategory>>();
mockRepo.Setup(r=>r.Add(It.IsAny<Category>())); //do not know or care what happens as this is a void method.
var sut= new CategoryService(null,mockRepo.Object,null,mockValidator.Object);
bool expected = false;
//ACT
bool actual = sut.AddCategory(new Category());
//ASSERT
mockRepo.Verify(r=>r.Add(It.IsAny<Category>(),Times.Exactly(1),"Repo ADD method not called or called too many times, etc");
Assert.AreEqual(expected,actual,"Add was called BUT the AddCategoryMethod didn't return true as expected"); //and of course you could be totally pedantic and create a new test method for that last assert ;)
}
The reason I favour this approach is because it forces you to consider the behaviour of the method under test, as well as ensuring that you don't involve any dependencies that are not being tested plus it means your test methods only create exactly what they need to in order to run the tests (and of course you can create some setup/teardown helpers to pre-create those mocks for you);
Of course you could put all the above into a single method but for the sake of saving a few LOC I hope you'll agree that having two separate tests to verify two separate behaviours is a more robust approach.
Just my 2c. hope it helps!
I want to create test case for below method "GetByEmail".
public User GetByEmail(string email, bool includeUserRoles = false, bool includeUserType = false)
{
Expression<Func<User>> whereClause = u => u.Email == email;
return GetQuery(whereClause, includeUserRoles, includeUserType) .FirstOrDefault();
}
private IQueryable<User> GetQuery(Expression<Func<User>> whereClause,
bool includeUserRoles = false, bool includeUserType = false)
{
IQueryable<User> query = base.GetQuery(whereClause);
if (includeUserRoles)
query = query.Include(u => u.UserRoles);
if (includeUserType)
query = query.Include(u => u.UserType);
return query;
}
protected IQueryable<T> GetQuery<T>(Expression<Func<T>> predicate) where T : EntityBase
{
return predicate != null ?
CreateObjectSet<T>().Where(predicate) :
CreateObjectSet<T>();
}
protected IObjectSet<T> CreateObjectSet<T>() where T : EntityBase
{
return _context.CreateObjectSet<T>();
}
public static IQueryable<T> Include<T>(this IQueryable<T> source, Expression<Func<T>> property)
{
var objectQuery = source as ObjectQuery<T>;
if (objectQuery != null)
{
var propertyPath = GetPropertyPath(property);
return objectQuery.Include(propertyPath);
}
return source;
}
Below is my test case method -
[Fact]
private void GetByEmail_PassedEmailAddress_RelatedUser()
{
//Created fake context
var fakeContext = Isolate.Fake.Instance<Entities>();
//Created fake Repository and passed fakeContext to it
var fakeRepository = Isolate.Fake.Instance<Repository>(Members.CallOriginal, ConstructorWillBe.Called, fakeContext);
//Created fake in memory collection of User
var fakeUsers = GetUsers();
Isolate.WhenCalled(() => fakeContext.Context.CreateObjectSet<User>())
.WillReturnCollectionValuesOf(fakeUsers);
var User = Isolate.Invoke.Method(fakeRepository, "GetByEmail", "abc#xyz.com", true, true);
Assert.True(User != null);
}
In the above test case method I successfully get the user with passed email but not able to include other entities of associated user.
Kindly let me know, how can I include other entities with associated User.
Include is leaky abstraction - it works only with EF and linq-to-entities and cannot be successfully used with linq-to-objects. You know that your unit test needs populated relations so your GetUsers method must prepare that data. That is a point of mocking / faking - you don't think about internal implementation of mocked method. You simply return what should be returned.
Btw. what is the point of your test? It looks like you are trying to test a mock - that is wrong. Mock provides correct data and you only need it to test another feature dependent on mocked component.