Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用mock的单元测试_C#_Json_Unit Testing - Fatal编程技术网

C# 使用mock的单元测试

C# 使用mock的单元测试,c#,json,unit-testing,C#,Json,Unit Testing,我正在尝试对我的应用程序进行单元测试。 这是我的测试: class CustomerControllerTest { [Test] public void GetAuctionsByJson_works() { // Arrange Mock<IAuctionRespository> mockAuction = new Mock<IAuctionRespository>(); mockAuction.

我正在尝试对我的应用程序进行单元测试。 这是我的测试:

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);

    }
}
class CustomerController测试
{
[测试]
public void GetAuctionsByJson_works()
{
//安排
Mock mockAuction=新Mock();
mockAuction.Setup(m=>m.Auctions).Returns(新拍卖[]
{
新拍卖{a_id=1,auctionname=“computer”,deadLine=DateTime.Today},
新拍卖{a_id=2,auctionname=“keyboard”,deadLine=DateTime.Today},
新拍卖{a_id=3,auctionname=“mouse”,deadLine=DateTime.Today}
}.AsQueryable());
CustomerController控制器=新的CustomerController(mockAuction.Object);
var actual=controller.GetAuctionsByJson()作为JsonResult;
//解析结果
列表
但我得到的结果是空的
为什么?
如何获取列表中的实际值?

谢谢

谢谢Andre!我的解决方案是添加以下行:

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

谢谢Andre!我的解决方案是添加以下行:

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

我想有两种可能的答案。1.实际值。数据是列表类型的nof(本例中的关键字as dosnt抛出异常)2.实际值。数据是某种json对象,您需要首先反序列化,您不能直接强制转换它(这就是我要说的问题)谢谢!失败是因为缺少反序列化。我已经按照你说的做了,并且成功了。我想有两个可能的答案。1.实际。数据是列表类型的nof(本例中的关键字as dosnt throw a exception)2.实际。数据是某种json对象,你需要先反序列化,你不能直接强制转换它(这是我要说的问题)谢谢!失败是因为缺乏反序列化。我已经照你说的做了,而且成功了。