Reputation: 121
I am currently writing unit test using nunit. To do this i get data from the database and pass through the method that i want to test, then i compare database table data are same as the method return data. I have a doubt is this good to do unit test? Can someone explain this? I have tried moq test but it has some problem with my methods. sometimes findasync not work for return list in Moq test. then i giveup it and turned to nunit test. I write unit test for entity framework.
[Test]
public async Task GetFirmSectionListAsync_Count_AreEqual()
{
var fICount = _clientDbContext.FITypes.ToList().Count;
var fIResult = await _firmService.GetFISListAsync(new CancellationToken(), Startup.ConnString);
Assert.AreEqual(fICount, fIResult.Entities.Count);
}
Upvotes: 3
Views: 1430
Reputation: 17658
To try to answer your question, I'll quote the various parts:
To do this i get data from the database and pass through the method that i want to test, then i compare database table data are same as the method return data. I have a doubt is this good to do unit test? Can someone explain this?
Your way of unit testing is not really good practice. Let me explain. There are 2 things to consider here. Let me start with the first one.
FirstNormally you would like to have a unit test in which the input and output are fixed, or at least independent of an external datasource like tables/rows/records in a database. So, in general it's better, and easier to create your data-set in memory, in your unit test.
SecondYou need to be aware or what it is exactly what you want to test. In your case it seems ike you are testing the Entity Framework, which you might want to consider already tested.
I have tried moq test but it has some problem with my methods. sometimes findasync not work for return list in Moq test. then i giveup it and turned to nunit test. I write unit test for entity framework.
So, I think it's better to focus on these problems. and: "write unit test for entity framework" seems overkill unless you are developping it.
So, what would be a good thing to test?
Well you methods like GetFirmSectionListAsync
perhaps. Maybe there is some internal logic going on, which you can test. But to do so, use a static dataset which covers all the logic scenario's. You don't twant your test/build to fail because somebody deleted some records.
Upvotes: 1