0

我正在使用 VS2012 中的 NSubstitute 运行测试,并且我正在尝试将模型转换为列表,或者我可以计算的东西。这是代码:

    [TestMethod]
    public void StartedGame()
    {
        // arrange
        var repo = Substitute.For<IGameRepository>();
        IEnumerable<Game> expectedGames = new List<Game> 
            {
                new Game{ gameTypeID = 1, hasStarted = false, ID = 1, isOver = false, isPublic = true, maxPlayers = 2, someoneReported = false},
                new Game{ gameTypeID = 1, hasStarted = false, ID = 2, isOver = false, isPublic = true, maxPlayers = 2, someoneReported = false},
                new Game{ gameTypeID = 2, hasStarted = false, ID = 3, isOver = false, isPublic = true, maxPlayers = 2, someoneReported = false},
                new Game{ gameTypeID = 2, hasStarted = false, ID = 4, isOver = false, isPublic = true, maxPlayers = 2, someoneReported = false},
                new Game{ gameTypeID = 1, hasStarted = false, ID = 5, isOver = false, isPublic = true, maxPlayers = 2, someoneReported = false}
            };
        repo.GetGames().Returns(expectedGames);
        var sut = new GameController(repo);

        // act
        var actual = sut.GameMenuStart();
        // assert
        //Assert.IsInstanceOfType(actual, typeof(ViewResult));
        var viewResult = actual as ViewResult;

        Assert.IsNotNull(viewResult);
    }

当我调试时,我可以将鼠标悬停在“viewResult”上并看到模型,在那里我看到条目和“计数”,但我怎样才能达到它?我试过像这样投射到“游戏”对象:

Game[] theModel = (Game[])viewResult.Model;

并列出等等,但它总是失败。有任何想法吗?

编辑:viewResult 的类型:System.Web.Mvc.ViewResult

这是此函数中的其余代码:

        var repo = Substitute.For<IGameRepository>();
        IEnumerable<Game> expectedGames = new List<Game> 
            {
                new Game{ gameTypeID = 1, hasStarted = false, ID = 1, isOver = false, isPublic = true, maxPlayers = 2, someoneReported = false},
                new Game{ gameTypeID = 1, hasStarted = false, ID = 2, isOver = false, isPublic = true, maxPlayers = 2, someoneReported = false},
                new Game{ gameTypeID = 2, hasStarted = false, ID = 3, isOver = false, isPublic = true, maxPlayers = 2, someoneReported = false},
                new Game{ gameTypeID = 2, hasStarted = false, ID = 4, isOver = false, isPublic = true, maxPlayers = 2, someoneReported = false},
                new Game{ gameTypeID = 1, hasStarted = false, ID = 5, isOver = false, isPublic = true, maxPlayers = 2, someoneReported = false}
            };
        repo.GetGames().Returns(expectedGames);
        var sut = new GameController(repo);
4

2 回答 2

2

var theList = viewResult.Model as IEnumerable<Game>;

这成功了。

于 2013-05-14T18:16:57.663 回答
0

由于它是一个 IEnumerable,如果您希望它是一个数组,您可以使用“ToArray”方法。

Game[] theModel = viewResult.ToArray();

然而,.Count应该只是工作?

我们能看到“ repo.GetGames()”的签名吗

于 2013-05-14T17:39:22.377 回答