Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: / ky.emrah
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Error when testing xUnit inserting a user into the database
I'm new to writing unit testing
I'm trying to write a unit test for inserting a user into a database but I'm getting an error
Error: System.NotSupportedException : Type to mock (SqlConnection) must be an interface, a delegate, or a non-sealed, non-static class.
I have a UserRepository class:
public class UserRepository : IUSerRepository
{
public async Task int InsertUserAsync(User user, SqlConnection connection, SqlTransaction transaction)
{
if (!await UserExistAsync(user.Email!, connection, transaction))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO users (FLO, email) VALUES (@Flo, @Email);SELECT SCOPE_IDENTITY();", connection, transaction))
{
cmd.Parameters.AddWithValue("@Flo", user.Flo);
cmd.Parameters.AddWithValue("@Email", user.Email);
object? res = await cmd.ExecuteScalarAsync();
return Convert.ToInt32(res);
}
}
else
{
using (SqlCommand cmd = new SqlCommand("SELECT user_id FROM users WHERE email = @Email", connection, transaction))
{
cmd.Parameters.AddWithValue("@Email", user.Email);
object? res = await cmd.ExecuteScalarAsync();
return Convert.ToInt32(res);
}
}
}
public async Task bool UserExistAsync(string userEmail, SqlConnection connection, SqlTransaction transaction)
{
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(1) FROM users WHERE email = @Email", connection, transaction))
{
cmd.Parameters.AddWithValue("@Email", userEmail);
int count = Convert.ToInt32(await cmd.ExecuteScalarAsync());
return count 0;
}
}
}
public class UserRepository : IUSerRepository
{
public async Task int InsertUserAsync(User user, SqlConnection connection, SqlTransaction transaction)
{
if (!await UserExistAsync(user.Email!, connection, transaction))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO users (FLO, email) VALUES (@Flo, @Email);SELECT SCOPE_IDENTITY();", connection, transaction))
{
cmd.Parameters.AddWithValue("@Flo", user.Flo);
cmd.Parameters.AddWithValue("@Email", user.Email);
object? res = await cmd.ExecuteScalarAsync();
return Convert.ToInt32(res);
}
}
else
{
using (SqlCommand cmd = new SqlCommand("SELECT user_id FROM users WHERE email = @Email", connection, transaction))
{
cmd.Parameters.AddWithValue("@Email", user.Email);
object? res = await cmd.ExecuteScalarAsync();
return Convert.ToInt32(res);
}
}
}
public async Task bool UserExistAsync(string userEmail, SqlConnection connection, SqlTransaction transaction)
{
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(1) FROM users WHERE email = @Email", connection, transaction))
{
cmd.Parameters.AddWithValue("@Email", userEmail);
int count = Convert.ToInt32(await cmd.ExecuteScalarAsync());
retSource of the question:
https://stackoverflow.com/questions/7...
Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/
Информация по комментариям в разработке