Unit test with Moq - asp.net-mvc-3

I am using Moq for unit testing, and I am trying to write my first unit test. My layers are "Controller=>Service=>Repository".
(I am using unity and repository pattern.)
Whenever I run my unit test, the actual value is always 0 like _service.GetEquipStates().Count() = 0. I do not know where I am doing wrong. Please suggest.
My unit test code is the following one:
private ITestService _service;
private Mock<ITestRepository> RepositoryMoc;
[TestInitialize]
public void Initialize() {
RepositoryMoc= new Mock<ITestRepository>();
_service = new TestService(RepositoryMoc.Object)
}
[TestMethod]
public void GetEquipmentState() {
var stateList = new[] { new State { ID = 1, Desc= "test" } };
RepositoryMoc.Setup(es => es.GetStates(true)).Returns(stateList );
Assert.AreEqual(1, _service.GetStates().Count());
}

Your setup is done for the methode GetState with prameter true.
RepositoryMoc.Setup(es => es.GetStates(true)).Returns(stateList);
But your call in the Assert-Statement is for a method GetState without a parameter. Is the method GetState declared with a default parameter or do you have to functions (one with a bool parameter and one without)?
Just make your call in the assert-statement like this and it should work.
Assert.AreEqual(1, _service.GetStates(true).Count());

I have replicated your code in one of my solutions, and the test passes fine.
private Mock<IAccessor> RepositoryMoc;
private Controller _service;
[TestMethod]
public void TestMethod()
{
// Arrange
_service = new Controller();
RepositoryMoc = new Mock<IAccessor>();
_service.Accessor = RepositoryMoc.Object;
var stateList = new[] { new State { ID = 1, Desc = "test" } };
RepositoryMoc.Setup(es => es.GetStates(true)).Returns(stateList);
// Act & Assert
Assert.AreEqual(1, _service.GetStates().Count());
}
Is the code exactly as is in your solution ?

Related

Spring unit tests [webflux, cloud]

I am new to the topic of unit testing and my question is whether I should perform the test as such of each line of code of a method or in what ways I can perform these tests to have a good coverage, if also, should exceptions be evaluated or not?
If for example I have this service method that also uses some helpers that communicate with other microservices, someone could give me examples of how to perform, thank you very much.
public Mono<BankAccountDto> save(BankAccountDto bankAccount) {
var count = findAccountsByCustomerId(bankAccount.getCustomerId()).count();
var customerDto = webClientCustomer
.findCustomerById(bankAccount.getCustomerId());
var accountType = bankAccount.getAccountType();
return customerDto
.zipWith(count)
.flatMap(tuple -> {
final CustomerDto custDto = tuple.getT1();
final long sizeAccounts = tuple.getT2();
final var customerType = custDto.getCustomerType();
if (webClientCustomer.isCustomerAuthorized(customerType, accountType, sizeAccounts)) {
return saveBankAccountAndRole(bankAccount);
}
return Mono.error(new Exception("....."));
});
}
EDIT
public Mono<BankAccountDto> save(BankAccountDto bankAccount) {
var count = findAccountsByCustomerId(bankAccount.getCustomerId()).count();
var customerDto = webClientCustomer
.findCustomerById(bankAccount.getCustomerId());
return customerDto
.zipWith(count)
.flatMap(tuple -> {
final var customDto = tuple.getT1();
final var sizeAccounts = tuple.getT2();
final var accountType = bankAccount.getAccountType();
// EDITED
return webClientCustomer.isCustomerAuthorized(customDto, accountType, sizeAccounts)
.flatMap(isAuthorized -> {
if (Boolean.TRUE.equals(isAuthorized)) {
return saveBankAccountAndRole(bankAccount);
}
return Mono.error(new Exception("No tiene permisos para registrar una cuenta bancaria"));
});
});
}
Given that you want to unit test this code, you would need to mock dependencies such as webClientCustomer.
Then you should always test whatever are the relevant paths within the code. Looking at your code I only see three relevant ones to be tested:
the method returns an empty Mono if webClientCustomer.findCustomerById(bankAccount.getCustomerId()); returns an empty Mono;
saveBankAccountAndRole(bankAccount) is called and your save() method actually returns whatever saveBankAccountAndRole(bankAccount) returns. This would should happen if webClientCustomer.isCustomerAuthorized(customerType, accountType, sizeAccounts) is true;
the method returns an exception if webClientCustomer.isCustomerAuthorized(customerType, accountType, sizeAccounts) is false.

Unit Test The instance of entity type cannot be tracked because another instance with the same key value for {'Id'} is already being tracked

I am writing a webAPI unittest that have several test call for each method in the class. I am using UseInMemoryDatabase to create a database and loading test data.
I was getting the following error between each test method
The instance of entity type 'AppSettings' cannot be tracked because another instance with the same key value for {'myId '} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached
I added context.Database.EnsureDeleted(); and that removed the error for the Get method but it was still throwing the error in the delete and update unit test.
I can't figure how to removed this error for delete and update when I run all the test methods at once.
Any help is appreciated.
private MyDBContext context;
[TestInitialize]
public void SetUp()
{
var options = new DbContextOptionsBuilder<MyDBContext>()
.UseInMemoryDatabase(databaseName: "MyDB")
.Options;
context = new CACIDBContext(options);
context.Database.EnsureDeleted(); // this removed the error between the several GET test methods. but not the delete,update
context.AppSettings.Add(new AppSettings { myId = 1, MyName = "test1", MyValue = "test1" });
context.AppSettings.Add(new AppSettings { myId = 2, MyName = "test2", MyValue = "test2" });
context.AppSettings.Add(new AppSettings { myId = 3, MyName = "test3", MyValue = "test3" });
context.SaveChanges();
}
[TestMethod]
public void GetSettings()
{
var logger = new Mock<ILogger<SettingsRepository>>();
var caciDbContent = new Mock<CACIDBContext>();
sRepository settingRepository = null;
settingRepository = new sRepository (context, logger.Object);
List<AppSettings> settings = settingRepository.GetSettings().ToList();
Assert.AreEqual(3, settings.Count);
}
[TestMethod]
public void RemoveSetting()
{
var logger = new Mock<ILogger<sRepository>>();
var caciDbContent = new Mock<CACIDBContext>();
sRepository sRepository = null;
sRepository = new sRepository (context, logger.Object);
// test Get By AppSettingName
bool result = sRepository.RemoveSetting(new AppSettings { myId = 3, MyName = "test3", MyValue = "test3");
Assert.AreEqual(true, result);
}
Here is my c# code
public bool RemoveSetting(AppSettings setting)
{
try
{
myDbContent.AppSettings.Remove(setting);
}
catch (Exception ex)
{
logger.LogError($"Failed to removfe an existing appsetting: {ex}");
throw ex;
}
return (myDbContent.SaveChanges() > 0 ? true : false);
}
Adding the following line helped in my case, which is similar to yours.
context.ChangeTracker.Clear()
It is only available for EF Core 5.0 though. Source: Microsoft Docs

Unit Tests on Method that uses GetEntitiesAync (DocumentDB)

After mocking DocumentDBRepository class with its GetEntitiesAsync() method in the unit test, it is return a null value which is not I expect it to return.
Here's my method that I need to test
public async Task<Books> GetBooksByBookIdAsyncByLinq(string bookId)
{
var books = await _individualRepository.GetEntitiesAsync(t => t.BookID == bookId);
if (individualResponse.Any())
{
return individualResponse.FirstOrDefault();
}
return null;
}
Here's the unit test of this method, noticed that I set up the GetEntitiesAsync() method and expect it to return a book value. But it is returning null when I ran it:
[Fact]
public void Test_GetBooksByBookIdAsyncByLinq()
{
//Arrange
var bookID = "000";
//Mock DocumentDBRepository
var mockDocumentDBRepository = new Mock<IRepository<Book>>();
var expected = Get_BookValue();
mockDocumentDBRepository.Setup(x => x.GetEntitiesAsync(x => x.BookID == bookID))
.Returns(Task.FromResult(expected));
var component = new BookComponent(mockDocumentDBRepository.Object);
//Act
var result = component.GetBooksByBookIdAsyncByLinq(bookID);
//Assert
result.Result.Should().NotBeNull().And.
BeOfType<Book>();
}
private Book Get_BookValue(){
IEnumerable<Book> result = new List<Book>
{ new Book
{ BookID = "000", BookName = "TestSourceSystemName" } };
return result;
}
When I ran the unit test and debug inside the GetBooksByBookIdAsyncByLinq() method, it is not getting any results from the books variable and return null without any error.
The interesting thing is, when I change the GetEntitiesAsync() method to RunSQLQueryAsync() method, which means using SQL query instead of Linq, the unit test is returning the correct result.
Here's the method I am testing:
public async Task<Books> GetBooksByBookIdAsyncBySQL(string bookId)
{
var books = await _individualRepository.RunSQLQueryAsync("select * from c where c.BookID ==" + bookId);
if (individualResponse.Any())
{
return individualResponse.FirstOrDefault();
}
return null;
}
And here's the unit test for this method, noticed that I set up the RunQueryAsync() method and expect to return a book value. And it works:
[Fact]
public void Test_GetBooksByBookIdAsyncBySQL()
{
//Arrange
var bookID = "000";
var sqlQuery = "select * from c where c.BookID ==" + bookId;
//Mock DocumentDBRepository
var mockDocumentDBRepository = new Mock<IRepository<Book>>();
var expected = Get_BookValue();
//mockDocumentDBRepository.Setup(x => x.GetEntitiesAsync(x => x.BookID == bookID))
// .Returns(Task.FromResult(expected));
mockDocumentDBRepository.Setup(x => x.RunQueryAsync(sqlQuery))
.Returns(Task.FromResult(expected));
var component = new BookComponent(mockDocumentDBRepository.Object);
//Act
var result = component.GetBooksByBookIdAsyncBySQL(bookID);
//Assert
result.Result.Should().NotBeNull().And.
BeOfType<Book>();
}
private Book Get_BookValue(){
IEnumerable<Book> result = new List<Book>
{ new Book
{ BookID = "000", BookName = "TestSourceSystemName" } };
return result;
}
So I am thinking maybe the way I mock GetEntitiesAsync() method is incorrect. But I am not sure why...
Here are the RunQueryAsync() and GetEntitiesAsync() methods for reference:
public async Task<IEnumerable<T>> GetEntitiesAsync(Expression<Func<T, bool>> predicate)
{
IDocumentQuery<T> query = GetQueryByPredicate(predicate);
List<T> results = new List<T>();
while (query.HasMoreResults)
{
results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
}
public async Task<IEnumerable<T>> RunQueryAsync(string queryString)
{
IDocumentQuery<T> query = GetQueryBySQL(queryString);
List<T> results = new List<T>();
while (query.HasMoreResults)
{
results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
}
The Setup method tries to look at the arguments passed into your method, and only match the specified behavior if those arguments are the same.
When you use GetQueryBySQL, Moq is able to detect that the sqlQuery string is the same (as in object.Equals()) as what's passed in, so it works correctly.
When you use GetEntitiesAsync, Moq looks at the two Expressions and thinks they are different because Expression comparison is based on memory equality. So even though the two x => x.BookID == bookIDs look the same to you and me, they are different Expressions at runtime.
Try using It.IsAny<>() instead:
mockDocumentDBRepository
.Setup(x => x.GetEntitiesAsync(It.IsAny<Expression<Func<Book, bool>>()))
.Returns(Task.FromResult(expected));
Supposing you get that working, you can use Callback or other strategies to test that the expression passed into GetEntitiesAsync has the behavior you're expecting it to have.

ReactiveUI not responding to changes in test (with Autofixture)

Look at the following codes (simple ones)
public class NewEventViewModel : ReactiveObject
{
readonly ObservableAsPropertyHelper<bool> isSendVisible;
public bool IsSendVisible
{
get { return isSendVisible.Value; }
}
private ReactiveList<string> users;
public ReactiveList<string> Users
{
get { return this.users; }
set { this.RaiseAndSetIfChanged(ref users, value); }
}
public NewEventViewModel()
{
Users = new ReactiveList<string>(new List<string>())
{
ChangeTrackingEnabled = true
};
Users.CountChanged
.Select(x => x > 0)
.ToProperty(this, x => x.IsSendVisible, out isSendVisible);
//Users.Add("commented");
}
}
And the test:
[Fact]
public void ShouldBeVisibleWhenIsAtLeastOneUser()
{
//var sut = new NewEventViewModel();
var fixture = new Fixture();
var sut = fixture.Create<NewEventViewModel>();
sut.Users.Add("something");
sut.IsSendVisible.ShouldBeTrue();
}
The test fails... but when I uncomment the line from ViewModel, it passes.
Looks like the test is ignoring changes to Users. It works when I create the sut manually (new NewEventViewModel()). Why is AutoFixture breaking the test in such a strange way? What am I doing wrong?
You can use
fixture.Build<NewEventViewModel>().OmitAutoProperties().Create()
to temporarily turn off auto-properties.

Need Help Writing Unit Test that on Method that requires HttpContext.Current.User

I am trying to write some unit tests for my MVC3 project (the first tests for this project) and I am getting stumped. Basically, I have a MemberQueries class that is used by my MemberController to handle all the logic.
I want to start writing tests on this class and want to start with a simple example. I have a method in this class called IsEditModeAvailable which determines if the user is a member of the "Site Administrator" role or that the user is able to edit their own data, but no one elses. I determine the last requirement by comparing the passed in Id value to the HttpContext User property.
The problem that I'm running into is I don't know how to mock or inject the proper parameters into my unit tests when creating the MemberQueries object. I am using, NUnit, Moq and Ninject, but I'm just not sure how to write the code. If I'm just not structuring this properly, please let me know as I'm a complete noob to unit testing.
Here's a sample of the code from my MemberQueries class:
public class MemberQueries : IMemberQueries
{
private readonly IRepository<Member> _memberRepository;
private readonly IMemberServices _memberServices;
private readonly IPrincipal _currentUser;
public MemberQueries(IUnitOfWork unitOfWork, IMemberServices memberServices, IPrincipal currentUser)
{
_memberRepository = unitOfWork.RepositoryFor<Member>();
_memberServices = memberServices;
_currentUser = currentUser;
}
public bool IsEditModeAvailable(int memberIdToEdit)
{
if (_currentUser.IsInRole("Site Administrator")) return true;
if (MemberIsLoggedInUser(memberIdToEdit)) return true;
return false;
}
public bool MemberIsLoggedInUser(int memberIdToEdit)
{
var loggedInUser = _memberServices.FindByEmail(_currentUser.Identity.Name);
if (loggedInUser != null && loggedInUser.Id == memberIdToEdit) return true;
return false;
}
}
Here's a sample from my MemberServices class (which is in my domain project, referenced by MemberQueries):
public class MemberServices : IMemberServices
{
private readonly IRepository<Member> _memberRepository;
public MemberServices(IUnitOfWork unitOfWork)
{
_memberRepository = unitOfWork.RepositoryFor<Member>();
}
public Member Find(int id)
{
return _memberRepository.FindById(id);
}
public Member FindByEmail(string email)
{
return _memberRepository.Find(m => m.Email == email).SingleOrDefault();
}
}
Finally, here's the stub of the unit test I am trying to write:
[Test]
public void LoggedInUserCanEditTheirOwnInformation()
{
var unitOfWork = new UnitOfWork();
var currentUser = new Mock<IPrincipal>();
// I need to somehow tell Moq that the logged in user has a HttpContext.User.Name of "jdoe#acme.com"
var memberServices = new Mock<MemberServices>();
// I then need to tell Moq that it's FindByEmail("jdoe#acme.com") method should return a member with a UserId of 1
var memberQueries = new MemberQueries(unitOfWork, memberServices.Object, currentUser.Object);
// If the logged in user is "jdoe#acme.com" who has an Id of 1, then IsEditModeAvailable(1) should return true
Assert.IsTrue(memberQueries.IsEditModeAvailable(1));
}
It looks like you are trying to test the MemberQueries.IsEditModeAvailable method. You have 2 cases to cover here. The Site Administrators case and the case where there's a currently logged user whose id matches the one passed as argument. And since the MemberQueries class relies purely on interfaces you could mock everything:
[TestMethod]
public void EditMode_Must_Be_Available_For_Site_Administrators()
{
// arrange
var unitOfWork = new Mock<IUnitOfWork>();
var currentUser = new Mock<IPrincipal>();
currentUser.Setup(x => x.IsInRole("Site Administrator")).Returns(true);
var memberServices = new Mock<IMemberServices>();
var memberQueries = new MemberQueries(unitOfWork.Object, memberServices.Object, currentUser.Object);
// act
var actual = memberQueries.IsEditModeAvailable(1);
// assert
Assert.IsTrue(actual);
}
[TestMethod]
public void EditMode_Must_Be_Available_For_Logged_In_Users_If_His_Id_Matches()
{
// arrange
var unitOfWork = new Mock<IUnitOfWork>();
var currentUser = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
identity.Setup(x => x.Name).Returns("john.doe#gmail.com");
currentUser.Setup(x => x.Identity).Returns(identity.Object);
currentUser.Setup(x => x.IsInRole("Site Administrator")).Returns(false);
var memberServices = new Mock<IMemberServices>();
var member = new Member
{
Id = 1
};
memberServices.Setup(x => x.FindByEmail("john.doe#gmail.com")).Returns(member);
var memberQueries = new MemberQueries(unitOfWork.Object, memberServices.Object, currentUser.Object);
// act
var actual = memberQueries.IsEditModeAvailable(1);
// assert
Assert.IsTrue(actual);
}
Actually there's a third case you need to cover: you have a currently logged in user, who is not a Site Administrator and whose id doesn't match the one passed as argument:
[TestMethod]
public void EditMode_Should_Not_Be_Available_For_Logged_In_Users_If_His_Id_Doesnt_Match()
{
// arrange
var unitOfWork = new Mock<IUnitOfWork>();
var currentUser = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
identity.Setup(x => x.Name).Returns("john.doe#gmail.com");
currentUser.Setup(x => x.Identity).Returns(identity.Object);
currentUser.Setup(x => x.IsInRole("Site Administrator")).Returns(false);
var memberServices = new Mock<IMemberServices>();
var member = new Member
{
Id = 2
};
memberServices.Setup(x => x.FindByEmail("john.doe#gmail.com")).Returns(member);
var memberQueries = new MemberQueries(unitOfWork.Object, memberServices.Object, currentUser.Object);
// act
var actual = memberQueries.IsEditModeAvailable(1);
// assert
Assert.IsFalse(actual);
}
The good news is that you are passing the user as an IPrincipal into the code that needs it rather than referring to HttpContext.Current.User. All you should need to do is setup the mock IPrincipal so that it returns the vales you need for that test.
var mockIdentity = new Mock<IIdentity>();
mockIdentity.Setup(x => x.Name).Returns("joe#acme.com");
var mockPrincipal = new Mock<IPrincipal>();
mockPrincipal.Setup(x => x.Identity).Returns(mockIdentity.Object);

Resources