因此,基本上,我试图在程序中的一个类上运行单元测试,该类中的方法可以正常工作,但由于某种原因,我无法通过单元测试。这是我的课:public class Driver{ public DateTime Date { get; set; } public String Start { get; set; } public String End { get; set; } public int Distance { get; set; } public Driver(DateTime date, String start, String end, int distance) { this.Date = date; this.Start = start; this.End = end; this.Distance = distance; } public override String ToString() { return string.Format("Date: {0}, Start Postcode: {1}, End Postcode: {2}, Distance: {3}miles", this.Date.ToString("dd/MM/yyyy"), this.Start, this.End, this.Distance); }}}`这是我的测试课: [TestClass]public class Driverdetailstest{ private Driver TestDriver; [TestMethod] public void DriverDataTest() { DateTime date = new DateTime(2017, 12, 15); string start = ("SR47BB"); string end = ("SR47FY"); int distance = 10; var expected = string.Format("Date: {0}, Start Postcode: {1}, End Postcode: {2}, Distance: {3}miles", date.ToString("dd/MM/yyyy"), start, end, distance); TestDriver = new Driver(date, start, end, distance); Convert.ToString(TestDriver); Assert.AreEqual(expected, TestDriver); }}尽管字符串返回相同,但由于测试失败,我仍然收到此错误。Message: Assert.AreEqual failed. Expected:<Date: 15/12/2017, Start Postcode: SR47BB, End Postcode: SR47FY, Distance: 10miles (System.String)>. Actual:<Date: 15/12/2017, Start Postcode: SR47BB, End Postcode: SR47FY, Distance: 10miles (TaxiTracker.Driver)>.我做错了什么?
1 回答
白板的微信
TA贡献1883条经验 获得超3个赞
单元测试的问题在于您正在比较string和object。
您应该执行以下任一操作:
Assert.AreEqual(expected, TestDriver.ToString());
或这个:
string testValue = Convert.ToString(TestDriver);
Assert.AreEqual(expected, testValue );
- 1 回答
- 0 关注
- 127 浏览
添加回答
举报
0/150
提交
取消