In Unit Testing the ASP.NET WebForm (Part 1), I showed you how to unit test the Create method in our data model or class. This time, I'll show you a simple way to unit test the Generic List<T> method in displaying records from the database and bind the result to GridView.
** Test Class **
[TestClass]
class ProfileTests
{
[TestMethod]
public void Get_All_Profile()
{
//Arrange
var profile = new List<Profile>();
//Act
var results = ProfileData.Get_All_Profile();
//Assert
Assert.IsTrue(profile.SequenceEqual(results));
Assert.IsNotNull(results);
}
}
** Data Model **
public static List<Profile> Get_All_Profile()
{
var profile = new List<Profile>();
using (var con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ProfileConnectionString"].ConnectionString))
{
string strSelect = "SELECT Name, Age FROM Profile";
var cmd = new SqlCommand(strSelect, con);
con.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
var result = new Profile
{
Name = reader.GetString(0),
Age = reader.GetDecimal(1)
};
profile.Add(result);
}
reader.Close();
}
return profile;
}
** Business Model **
public class Profile
{
public int ProfileId { get; set; }
public string Name { get; set; }
public decimal Age { get; set; }
}
** Code Behind **
void Display_Profile()
{
Profiles = ProfileData.Get_All_Profile();
GridView1.DataSource = Profiles.ToList();
GridView1.DataBind();
}
No comments:
Post a Comment