I have been working on alot of unit tests lately and have been doing alot of reading about them. I have some confusion on what specifically to implement/test in unit test cases.
Note: using FakeItEasy and NUnit
GOAL: For example I am testing this AccountManager. I am testing the methods this manager implements to ensure it returns what I want it to return:
My AccountManager:
public class AccountManager : IAccountManager
{
private readonly IAccountRepository _accountRepository;
public AdminController(IAccountRepository accountRepository)
{
_accountRepository = accountRepository;
}
//Method I am testing
List<Group> GetAll()
{
return _accountRepository.GetAllAccounts();
}
}
Option 1 Unit Test
[Test]
public void GetAllAccounts_ReturnListAccounts()
{
//Arrange
var mockAccountManager = A.Fake<IAccountManager>();
//Build Expected
List<Account> expected = new List<Account>{};
//Arrange Calls
A.CallTo(() => mockAccountManager.GetAll()).Returns(expected);
//Act
var returned = mockAccountManager.GetAll();
//Assert
A.CallTo(() => mockAccountManager.GetAll()).MustHaveHappened();
Assert.AreEqual(expected, returned);
}
Option 2 Unit Test
[Test]
public void GetAllAccounts_ReturnListAccounts()
{
//Arrange
var mockAccountManager = A.Fake<IAccountManager>();
var mockAccountRepository = A.Fake<IAccountReposiory>();
//Build Expected
List<Account> expected = new List<Account>{};
//Arrange Calls
A.CallTo(() => mockAccountRepository.GetAllAccounts()).Returns(expected);
//Act
var returned = mockAccountRepository.GetAllAccounts();
//Assert
A.CallTo(() => mockAccountRepository.GetAllAccounts()).MustHaveHappened();
Assert.AreEqual(expected, returned);
}
QUESTION:
When I test this method, is it best practice to set up my test in my 'Option 1' or 'Option 2' ?
Which one is correct and why? How can I improve?
Aucun commentaire:
Enregistrer un commentaire