public void GivenIdOf1ThenReturnAsynchronouslyGangMemberFromDbToMatchExpected() { // Instantiate the DataLayer which we are to use. DataLayer dataLayer = new DataLayer(); // Declare the actual gang member. GangMember actualMember = null; // Decide on which gang member we want from the database int memberID = 1; // Create a task this goes and gets us the gang member we are after on // a separate thread. Because I/O can take some time we can now move on // and do other stuff. Task<GangMember> actualMemberTask = dataLayer.GetGangMemberByIDAsync(memberID); // In the mean time while I/O is happening we can instantiate our // expected gang member, this is what we will test against. GangMember expectedMember = new GangMember() { MemberID = 1, MemberName = "Gentleman Jack", MemberRank = "Bootlegger" }; // We have created our expected gang member, I/O could still be occurring. // To block any further execution on the calling thread and force us to wait // until I/O finishes we call the .Result property (the same as the Wait method) // Note: We wouldn't want to call .Result before we instantiate the expected // member because this would defeat the purpose of doing async I/O. actualMember = actualMemberTask.Result; // Here we test that the expectedMember and actualMember match. Assert.AreEqual(expectedMember.MemberID, actualMember.MemberID); Assert.AreEqual(expectedMember.MemberName, actualMember.MemberName); Assert.AreEqual(expectedMember.MemberRank, actualMember.MemberRank); }
/// <summary> /// This method does the same job as the previous one, but this time doing things async. We are not required to /// do anything like this in the project, but it might be helpful when you come to this topic in your own studies. /// </summary> /// <param name="memberID"></param> /// <returns></returns> public async Task<GangMember> GetGangMemberByIDAsync(int memberID) { GangMember localGangMember = null; using (SqlConnection sqlConn = new SqlConnection(CONNECTIONSTRING)) { await sqlConn.OpenAsync(); string sqlQuery = "SELECT * FROM [Members] WHERE MemberID=@memberID"; using (SqlCommand sqlCmd = new SqlCommand(sqlQuery, sqlConn)) { sqlCmd.Parameters.AddWithValue("memberID", memberID); using (SqlDataReader sqlDataReader = await sqlCmd.ExecuteReaderAsync()) { while (await sqlDataReader.ReadAsync()) { localGangMember = new GangMember(); localGangMember.MemberID = sqlDataReader.GetInt32(0); localGangMember.MemberName = sqlDataReader.GetString(1); localGangMember.MemberRank = sqlDataReader.GetString(2); } } } } return localGangMember; }
public void GivenMemberIdOf1MethodReturnsValidGangMember() { DataLayer dataLayer = new DataLayer(); int memberID = 1; GangMember actualMember = dataLayer.GetGangMemberByMemberID(memberID); GangMember expectedMember = new GangMember() { MemberID = 1, MemberName = "Gentleman Jack", MemberRank = "Bootlegger" }; Assert.AreEqual(expectedMember.MemberID, actualMember.MemberID); Assert.AreEqual(expectedMember.MemberName, actualMember.MemberName); Assert.AreEqual(expectedMember.MemberRank, actualMember.MemberRank); }
/// <summary> /// This method returns all the gang members from the database and stores them into a collection. /// Notice how in the corresponding test we can loop through all the members to check that they match. /// </summary> /// <returns></returns> public List<GangMember> GetAllGangMembers() { List<GangMember> gangMembers = new List<GangMember>(); using (SqlConnection sqlConn = new SqlConnection(CONNECTIONSTRING)) { sqlConn.Open(); string sqlQuery = "SELECT * FROM [Members]"; using (SqlCommand sqlCmd = new SqlCommand(sqlQuery, sqlConn)) { using (SqlDataReader sqlDataReader = sqlCmd.ExecuteReader()) { while (sqlDataReader.Read()) { GangMember localGangMember = new GangMember(); localGangMember.MemberID = sqlDataReader.GetInt32(0); localGangMember.MemberName = sqlDataReader.GetString(1); localGangMember.MemberRank = sqlDataReader.GetString(2); gangMembers.Add(localGangMember); } } } } return gangMembers; }
public async Task<List<GangMember>> GetAllGangMembersAsync() { List<GangMember> gangMembers = new List<GangMember>(); using (SqlConnection sqlConn = new SqlConnection(CONNECTIONSTRING)) { await sqlConn.OpenAsync(); string sqlQuery = "SELECT * FROM [Members]"; using (SqlCommand sqlCmd = new SqlCommand(sqlQuery, sqlConn)) { using (SqlDataReader sqlDataReader = await sqlCmd.ExecuteReaderAsync()) { while (await sqlDataReader.ReadAsync()) { GangMember localGangMember = new GangMember(); localGangMember.MemberID = sqlDataReader.GetInt32(0); localGangMember.MemberName = sqlDataReader.GetString(1); localGangMember.MemberRank = sqlDataReader.GetString(2); // Comment out the following line of code to make the test fail, this was something that I forgot to do // and the unit test picked it up - reinforcing why tests are so helpful. gangMembers.Add(localGangMember); } } } } return gangMembers; }