I am having a hard time figuring out how to do unit testing in my MVC Web App. Although it is not a really hard concept to grasp, I was not able to find out whether I need a specific framework for this or respect some certain steps, given its multi-layered architecture. I would be very glad if you could give me some tips or code snippets so I could have an idea on what I should actually do. I will give you an example from my project:
PhaseController
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Phase phase)
{
int releaseId = (int)TempData["id"];
string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
string sql = "CreatePhase";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.CommandType = CommandType.StoredProcedure;
// adding parameters
SqlParameter parameter = new SqlParameter
{
ParameterName = "@Name",
Value = phase.Name,
SqlDbType = SqlDbType.VarChar,
Size = 50
};
command.Parameters.Add(parameter);
parameter = new SqlParameter
{
ParameterName = "@ReleaseId",
Value = releaseId,
SqlDbType = SqlDbType.Int
};
command.Parameters.Add(parameter);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
return RedirectToAction("Index", "Phase", new { id = releaseId });
}
Phase Model
namespace Intersection.Models
{
public class Phase
{
public int Id { get; set; }
public string Name { get; set; }
public int ReleaseId { get; set; }
// public IEnumerable<Phase> phases { get; set; }
}
}
Take these two as an example - how should my test class look like in order to properly test the Create() method (The POST one as well)?
EDIT:
Following @Nkosi's reply, I am adding a different method from another controller:
public IActionResult Create()
{
List<Intersection.Models.Environment> environmentList = new List<Intersection.Models.Environment>();
string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "ReadEnvironments";
SqlCommand command = new SqlCommand(sql, connection);
command.CommandType = CommandType.StoredProcedure;
using (SqlDataReader dataReader = command.ExecuteReader())
{
while (dataReader.Read())
{
Intersection.Models.Environment environment = new Intersection.Models.Environment();
environment.Id = Convert.ToInt32(dataReader["Id"]);
environment.Name = Convert.ToString(dataReader["Name"]);
environmentList.Add(environment);
}
}
ViewBag.Environments = environmentList;
return View();
}
}
[HttpPost]
public IActionResult Create(Application application)
{
string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
string sql = "CreateApplication";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.CommandType = CommandType.StoredProcedure;
// adding parameters
SqlParameter parameter = new SqlParameter
{
ParameterName = "@Name",
Value = application.Name,
SqlDbType = SqlDbType.VarChar,
Size = 50
};
command.Parameters.Add(parameter);
parameter = new SqlParameter
{
ParameterName = "@Environment",
Value = application.Environment,
SqlDbType = SqlDbType.Int
};
command.Parameters.Add(parameter);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
return RedirectToAction("Index", "Application");
}