[英]unit testing using mock
我正在嘗試對我的應用進行單元測試。 這是我的測試:
class CustomerControllerTest
{
[Test]
public void GetAuctionsByJson_works()
{
// Arrange
Mock<IAuctionRespository> mockAuction = new Mock<IAuctionRespository>();
mockAuction.Setup(m => m.Auctions).Returns(new Auction[]
{
new Auction { a_id=1, auctionname="computer", deadLine=DateTime.Today},
new Auction { a_id=2, auctionname="keyboard", deadLine=DateTime.Today},
new Auction { a_id=3, auctionname="mouse", deadLine=DateTime.Today}
}.AsQueryable());
CustomerController controller = new CustomerController(mockAuction.Object);
var actual = controller.GetAuctionsByJson() as JsonResult;
//parse result
List<Auction> result = actual.Data as List<Auction>; //null
Assert.AreEqual(3, result.Count);
Assert.AreEqual(1, result[0].a_id);
Assert.AreEqual("computer", result[0].auctionname);
Assert.AreEqual(DateTime.Today, result[0].deadLine);
}
}
var actual有值(如圖中所示) 實際值,但我得到的var結果是null 結果為什么? 我怎樣才能在列表中獲得實際值? 謝謝
謝謝安德烈! 我的解決方案是添加以下行:
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Auction> result = serializer.Deserialize<List<Auction>>(serializer.Serialize(actual.Data));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.