2012-03-05 305 views
0

我有一個返回MvcHtmlString,所以在我的單元測試我想,以確保正確的類型回來的方法:單元測試

[TestMethod] 
public void GetTeamNameByAlignmentTest() 
{  
    var target01 = MockEventData().First().GetTeamNameByAlignment("HoMe");  
    Assert.IsInstanceOfType(target01, typeof(MvcHtmlString)); 
} 

但是測試失敗

Assert.IsInstanceOfType failed. Expected type:<System.Web.Mvc.MvcHtmlString>. Actual type:<System.Web.Mvc.{Dynamic}.DynamicMvcHtmlString> 

如何正確測試MvcHtmlString返回類型?

編輯: 的方法:

public MvcHtmlString GetTeamNameByAlignment(String alignment) 
     { 
     StringBuilder teamName = new StringBuilder(); 
     if (Participants.First().Alignment.ToLower() == alignment.ToLower()) 
     { 
      teamName.Append(Participants.First().Team.TeamName); 
      if (Participants.First().Winning == true) 
      { 
       teamName.Insert(0, "<b>"); 
       teamName.Append("</b>"); 
      } 
     } 

     if (Participants.Last().Alignment.ToLower() == alignment.ToLower()) 
     { 

      teamName.Append(Participants.Last().Team.TeamName); 
      if (Participants.Last().Winning == true) 
      { 
       teamName.Insert(0, "<b>"); 
       teamName.Append("</b>"); 
      } 
     } 

     return MvcHtmlString.Create(teamName.ToString()); 
    } 

編輯2:

嘲笑數據:

private static IQueryable<Event> MockEventData() 
    { 
     Team team01 = new Team(); 
     team01.TeamName = "HomeTeam"; 

     Team team02 = new Team(); 
     team02.TeamName = "AwayTeam"; 


     EventParticipant participant01 = new EventParticipant(); 
     participant01.Alignment = "home"; 
     participant01.Team = team01; 
     participant01.Winning = false; 
     participant01.totalScore = 77; 

     EventParticipant participant02 = new EventParticipant(); 
     participant02.Alignment = "away"; 
     participant02.Team = team02; 
     participant02.Winning = true; 
     participant02.totalScore = 99; 


     Event event01 = new Event(); 
     event01.Participants = new List<EventParticipant> {participant01, participant02}; 

     IQueryable<Event> events = new List<Event> { event01 }.AsQueryable(); 

     return events; 
    } 
+1

'target01'是什麼? – 2012-03-05 14:55:49

+1

你能告訴我們你正在測試的方法嗎? – 2012-03-05 14:57:05

+0

什麼是你在單元測試中使用的'MockEventData'類?它返回什麼?它是如何實現的? – 2012-03-05 15:29:45

回答

1

看着this source codeMvcHtmlStringDynamicMvcHtmlString看起來是一個運行時創建的子類MvcHtmlString。考慮到這一點,如何改變你的主張:

Assert.IsTrue(target01 is MvcHtmlString); 

+0

輝煌,謝謝 – keeg 2012-03-05 18:50:30