How to write Unit test for DAL which call stored procedure and direct sql commands

Samuel David

Member
Joined
Nov 29, 2022
Messages
9
Programming Experience
Beginner
Hello Everyone,

I am writing unit test cases(using xunit) for DAL methods(which are calling stored procedures and some direct sql commands and not returning methods). How can I mock the results coming from db in unit tests.

sample code:
public class Notification : Base, INotificationBal
{
public void InsertNotification(NotificationType notif)
{
using(var con=GetConnction())
{
using(var command = CreatSqlCommand(con, "sp_InsertNotifDetails"))
{
//notif.NotificationType=1, notif.NotificationName="Mobile"
command.SetParamsValues(
("@NotificationType", notif.NotificationType),
("@NotificationName", notif.NotificationName)
));
command.ExecuteNonQuery());
}
}
}
}

Please let me know sample references or suggestions.

Thanks,
Sam
 
Last edited:
You not unit testing anymore because you are testing both the DAL and the stored procedure. A unit test tests just one thing at a time. What you are doing is the beginning of integration testing.

NVM: I see that your objective it to really implement a unit test. You will need to introduce dependency injection which emulates the database APIs -- in particular the DbCommand and DbDataReader.
 
Last edited:
You not unit testing anymore because you are testing both the DAL and the stored procedure. A unit test tests just one thing at a time. What you are doing is the beginning of integration testing.

NVM: I see that your objective it to really implement a unit test. You will need to introduce dependency injection which emulates the database APIs -- in particular the DbCommand and DbDataReader.
Thanks for responding. But how? I am not sure. Any reference to do this?
 
You need to learn to identify the key words in what people say and then search for information using those.
You will need to introduce dependency injection which emulates the database APIs
That's what you'll need to search for. It's topic, not just a detail, so you'll need to spend some time on it to understand it, then you can ask specific questions if you need to. It will require you to change the structure of your code a bit, and then any existing unit tests you have accordingly.
 
Back
Top Bottom