简体   繁体   中英

unit testing using mock

I'm trying to conduct unit test on my app. This is my test:

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 has values (as seen in image) actual val but i get that var result is null result val why? how can i get actual values in a list? Thank

Thanks Andre! My solution was to add this lines:

JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Auction> result = serializer.Deserialize<List<Auction>>(serializer.Serialize(actual.Data));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM