C'tor DataServiceQuery
是私人的,因此我不能用它来嘲笑它Moq
.
我使用MsFakes作为免费的代码编织工具来解决这个问题:
[TestMethod] public void LevelsVMConstructorTest() { using (ShimsContext.Create()) { Listlevels = new List (); levels.Add(new Level() { Id = 1, LevelId = 1, Name = "abc", IsRoot = true }); var levelsIQ = levels.AsQueryable(); var fakeDataServiceQuery = new System.Data.Services.Client.Fakes.ShimDataServiceQuery (); fakeDataServiceQuery.ProviderGet = () => levelsIQ.Provider; fakeDataServiceQuery.ExpressionGet = () => levelsIQ.Expression; fakeDataServiceQuery.ElementTypeGet = () => levelsIQ.ElementType; fakeDataServiceQuery.GetEnumerator = levelsIQ.GetEnumerator; var defaultContainerMock = new Mock (); defaultContainerMock.Setup(m => m.Levels).Returns(fakeDataServiceQuery); LevelViewer lv = new LevelViewer(odataContainerMock.Object); Assert.IsTrue(lv.GetRootLevels().Any()); } }