我有一个返回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;
}
答案 0 :(得分:1)
查看MvcHtmlString
的{{3}},DynamicMvcHtmlString
看起来是MvcHtmlString
的运行时创建的子类。考虑到这一点,如何将您的断言更改为:
Assert.IsTrue(target01 is MvcHtmlString);