我正在使用 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);