lundi 3 août 2015

How to test a method that returns an object which is a result of JOIN of two tables?

I have a method like this:

public StudentHistory GetStudentHistory(string studentId)
{
    StudentHistory _studentHistory = new StudentHistory();

    var query = (from SS in _courseCatalogContext.StudentSchedule
                 join C in _courseCatalogContext.Course on SS.Id equals C.ScheduleId
                 where SS.StudentId == studentId
                 orderby j.ReceivedDateTime

                 select new
                 {
                     SS.StudentId,
                     SS.Label,
                     SS.Status,
                     SS.ScheduledDate,
                     C.ReceivedDateTime,
                     C.ContentId
                 }).FirstOrDefault();


    if (query != null)
    {
        _studentHistory.StudentId = query.StudentId;
        _studentHistory.CourseLabel = query.Label;
        _studentHistory.CourseStatus = query.Status;
        _studentHistory.CourseStartDate = query.ScheduledDate;
        _studentHistory.CourseId = query.ContentId;
    }

    return _studentHistory;
}

This method does two things:

  1. Uses EF to join to tables and return an object

  2. Uses object from #1 to create a another object and return that

So how do I test these two or just do this:

[TestMethod]
public void Calling_GetStudentHistory_Should_Return_A_SingleStudentHistoryObject()
{
    StudentHistory _studentHistory = new StudentHistory()
    {
        StudentId = "test-4545456-sfd56",
        CourseLabel = "2",
        CourseStatus = "Not Started",
        CourseStartDate = "2015-03-31 00:00:00.0000000",
        CourseId = "test"
    };

    var _studentHistoryObject = _updateStudentRepository.GetStudentHistory("test");
    Assert.AreEqual("test", _studentHistory.CourseId);
}

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire