C
C#9mo ago
Rowin ツ

Unit testing FakeItEasy

I'm currently working on a project and I want to implement Unit tests for certain cases. After some research I decided to use FakeItEasy, but honestly I have NO clue what and how I am supposed to write out certain cases. The following example is some test I at least managed to write without having any errors and having succeeded after running the test. Though I should get a list of Dossiers back, I checked the result using Debugging and it should return a full list. Is there anyone that could briefly help me figure out how this "mocking" works and how I should write out this case?
public class DossierControllerTests
{
[Fact]
public void DossierController_GetAllDossiers_ReturnsFullList()
{
int id = 3;
var repo = A.Fake<IRepository<Dossier>>();

var result = A.CallTo(() => repo.GetAllByIntId(id)).Returns(new List<Dossier>());
}
}
public class DossierControllerTests
{
[Fact]
public void DossierController_GetAllDossiers_ReturnsFullList()
{
int id = 3;
var repo = A.Fake<IRepository<Dossier>>();

var result = A.CallTo(() => repo.GetAllByIntId(id)).Returns(new List<Dossier>());
}
}
No description
2 Replies
jcotton42
jcotton429mo ago
you're not testing anything there think about what that test is actually doing you're making a mock of a repo, then just calling that mock it's just testing the mock I personally would recommend you lean towards integration tests, leaving mocks for external services that cannot realistically be part of your tests eg in a Discord bot you might mock the Discord API, but everything else (database, etc.) is real in some way in the case of databases, there are two main options - if you're using sqlite, just use it's in-memory mode - otherwise (for eg postgres, ms sql server) use https://dotnet.testcontainers.org/ if you're using EF, do not use it's inmemory database (not to be confused with sqlite's inmemory mode), as it's lacking extremely basic features like transactions, and gets basically no development afaict
Rowin ツ
Rowin ツOP9mo ago
Alright thank you for the feedback!
Want results from more Discord servers?
Add your server