本文整理汇总了C#中Mock.SetupUser方法的典型用法代码示例。如果您正苦于以下问题:C# Mock.SetupUser方法的具体用法?C# Mock.SetupUser怎么用?C# Mock.SetupUser使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mock
的用法示例。
在下文中一共展示了Mock.SetupUser方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Index_Privileged_NotForSaleGame
public async void Index_Privileged_NotForSaleGame(string role)
{
List<Game> games = new List<Game>
{
new Game { GameAvailabilityStatus = AvailabilityStatus.NotForSale },
new Game { GameAvailabilityStatus = AvailabilityStatus.Available }
};
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(games.AsQueryable());
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().IsInRole(role);
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
var result = await controller.Index() as ViewResult;
Assert.That(result != null);
Assert.That(result.Model, Is.InstanceOf<GameListViewModel>());
var model = (GameListViewModel)result.Model;
Assert.That(model.Games, Has.Count.EqualTo(2));
Assert.That(model.Games, Is.EqualTo(games));
}
示例2: DeleteGame_ValidDeleteNoGameProduct
public async void DeleteGame_ValidDeleteNoGameProduct(string role)
{
Game aGame = new Game
{
Id = gameId,
GameSKUs = new List<GameProduct>()
};
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Game> { aGame }.AsQueryable());
gameDbSetStub.Setup(g => g.FindAsync(aGame.Id)).ReturnsAsync(aGame);
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().IsInRole(role);
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
var result = await controller.Delete(aGame.Id) as ViewResult;
Assert.That(result != null);
Assert.That(result.Model != null);
Assert.That(result.Model, Is.InstanceOf<Game>());
}
示例3: AdvancedSearch_EmptySearchParameters_ReturnsViewModelWithAllPlatforms
public async void AdvancedSearch_EmptySearchParameters_ReturnsViewModelWithAllPlatforms()
{
List<Platform> platforms = GetPlatformList();
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Platform>> platformDbSetStub = TestHelpers.GetFakeAsyncDbSet(platforms.AsQueryable());
dbStub.Setup(db => db.Platforms).Returns(platformDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().InAllRoles();
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
// Calling contains on the null Name would throw
var result = await controller.AdvancedSearch(null) as ViewResult;
Assert.That(result != null);
Assert.That(result.Model, Is.InstanceOf<AdvancedGameSearchViewModel>());
var model = (AdvancedGameSearchViewModel)result.Model;
Assert.That(model.Platforms, Has.Count.EqualTo(platforms.Count));
}
示例4: Details_IdInDb_ReturnsViewWithModel
public async void Details_IdInDb_ReturnsViewWithModel()
{
Game matchingGame = new Game
{
Id = gameId,
GameAvailabilityStatus = AvailabilityStatus.Available,
GameSKUs = new List<GameProduct>()
};
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Game> { matchingGame }.AsQueryable());
gameDbSetStub.SetupForInclude();
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().InAllRoles();
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
var result = await controller.Details(matchingGame.Id) as ViewResult;
Assert.That(result != null);
Assert.That(result.Model != null);
Assert.That(result.Model, Is.InstanceOf<Game>());
Assert.That(result.Model, Is.EqualTo(matchingGame));
}
示例5: AdvancedSearch_EmptySearchParameters_ReturnsAdvancedSearchView
public async void AdvancedSearch_EmptySearchParameters_ReturnsAdvancedSearchView()
{
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(GetGameSearchList().AsQueryable());
Mock<DbSet<Tag>> tagDbSetStub = TestHelpers.GetFakeAsyncDbSet(GetTagList().AsQueryable());
Mock<DbSet<Platform>> platformDbSetStub = TestHelpers.GetFakeAsyncDbSet(GetPlatformList().AsQueryable());
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
dbStub.Setup(db => db.Tags).Returns(tagDbSetStub.Object);
dbStub.Setup(db => db.Platforms).Returns(platformDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().InAllRoles();
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
// Calling contains on the null Name would throw
var result = await controller.AdvancedSearch(null) as ViewResult;
Assert.That(result != null);
Assert.That(result.ViewName, Is.Empty);
}
示例6: DeleteGame_ValidDeleteWithGameProduct
public async void DeleteGame_ValidDeleteWithGameProduct(string role)
{
Game aGame = new Game
{
Id = gameId
};
GameProduct aGameProduct = new PhysicalGameProduct();
aGameProduct.GameId = aGame.Id;
aGameProduct.Id = new Guid("44B0752E-968B-477A-AAAD-3ED535BA3559");
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Game> { aGame }.AsQueryable());
Mock<DbSet<GameProduct>> gameProductsDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<GameProduct> { aGameProduct }.AsQueryable());
gameDbSetStub.Setup(g => g.FindAsync(aGame.Id)).ReturnsAsync(aGame);
gameProductsDbSetStub.Setup(gp => gp.FindAsync(aGameProduct.Id)).ReturnsAsync(aGameProduct);
dbStub.Setup(db => db.GameProducts).Returns(gameProductsDbSetStub.Object);
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().IsInRole(role);
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
var result = await controller.Delete(aGame.Id) as ViewResult;
Assert.That(result != null);
Assert.That(result.Model != null);
Assert.That(result.Model, Is.InstanceOf<Game>());
}
示例7: Index_WhenCalled_ReturnsViewResult
public async void Index_WhenCalled_ReturnsViewResult()
{
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gamesDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Game> {futureGame1, futureGame2, pastGame1, pastGame2, pastGame3}.AsQueryable());
dbStub.Setup(db => db.Games).Returns(gamesDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().InAllRoles();
HomeController controller = new HomeController(dbStub.Object)
{
ControllerContext = contextStub.Object
};
var result = await controller.Index() as ViewResult;
Assert.That(result != null);
Assert.That(result.Model, Is.InstanceOf<HomePageViewModel>());
var model = (HomePageViewModel)result.Model;
Assert.That(model.ComingSoon.Select(g => g.GameSKUs), Has.All.Matches<List<GameProduct>>(gs => gs.Any(lgp => lgp.ReleaseDate > DateTime.Now)));
Assert.That(model.NewReleases.Select(g => g.GameSKUs), Has.All.Matches<List<GameProduct>>(gs => gs.Any(lgp => lgp.ReleaseDate < DateTime.Now)));
}
示例8: CreateReviewForGameProduct_POST_UserNotInMemberRole_RedirectsBackToGameDetail
public async void CreateReviewForGameProduct_POST_UserNotInMemberRole_RedirectsBackToGameDetail()
{
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().InNoRoles();
var idGetter = TestHelpers.GetSetupIUserIdGetterFake(memberId);
ReviewsController controller = new ReviewsController(dbStub.Object, idGetter.Object)
{
ControllerContext = contextStub.Object,
};
ReviewViewModel model = new ReviewViewModel()
{
GameId = game.Id,
GameSKUSelectList = null,
Review = fullReview
};
var result = await controller.CreateReviewForGameProduct(model) as RedirectToRouteResult;
Assert.That(result != null);
Assert.That(result.RouteValues["Action"], Is.EqualTo(nameof(GamesController.Details)));
Assert.That(result.RouteValues["Controller"], Is.EqualTo("Games"));
Assert.That(result.RouteValues["id"], Is.EqualTo(model.GameId));
}
示例9: AdvancedSearch_EmptyTitleKeyword_DoesNotFilterByName
public void AdvancedSearch_EmptyTitleKeyword_DoesNotFilterByName()
{
List<Game> games = new List<Game>
{
new Game
{
Name = null,
GameSKUs = GetGameSKUsListWithAllAvailabilityStatuses()
}
};
List<Platform> platforms = GetPlatformList();
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(games.AsQueryable());
Mock<DbSet<Platform>> platformDbSetStub = TestHelpers.GetFakeAsyncDbSet(platforms.AsQueryable());
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
dbStub.Setup(db => db.Platforms).Returns(platformDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().InAllRoles();
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
// Calling contains on the null Name would throw
Assert.That(async () => await controller.AdvancedSearch(null, platform: platforms.First().PlatformCode), Throws.Nothing);
}
示例10: Index_Pagination_ValidPages
public async void Index_Pagination_ValidPages([Range(1, 3)] int page)
{
List<Game> games = new List<Game>
{
new Game(),
new Game(),
new Game(),
};
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(games.AsQueryable());
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().InAllRoles();
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object,
GamesPerPage = 1
};
var result = await controller.Index(page) as ViewResult;
Assert.That(result != null);
Assert.That(result.Model, Is.InstanceOf<GameListViewModel>());
var model = (GameListViewModel)result.Model;
Assert.That(model.Games, Has.Count.EqualTo(1));
Assert.That(model.Games, Has.Member(games[page - 1]));
}
示例11: GetGameSKUsListWithAllAvailabilityStatuses
public async void Details_UserIsUnprivilegedRole_GameWithRatingMinimumAgeGreaterThanZero_CookieWithAgeLessThanMinimumAge_AddsAlert(string role)
{
Game matchingGame = new Game
{
Id = gameId,
GameAvailabilityStatus = AvailabilityStatus.PreOrder,
GameSKUs = GetGameSKUsListWithAllAvailabilityStatuses(),
Rating = matureESRBRating,
Name = "a game"
};
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Game> { matchingGame }.AsQueryable());
gameDbSetStub.SetupForInclude();
dbStub.
Setup(db => db.Games).
Returns(gameDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.
SetupUser().
IsInRole(role);
contextStub.
Setup(c => c.HttpContext.Request.Cookies).
Returns(new HttpCookieCollection
{
new HttpCookie(AgeGateController.DATE_OF_BIRTH_COOKIE, DateTime.MaxValue.ToShortDateString())
});
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
await controller.Details(matchingGame.Id);
Assert.That(controller.TempData[AlertHelper.ALERT_MESSAGE_KEY],
Has.Some.Matches<AlertMessage>(am => am.Message == AgeGateController.AgeBlockMessage));
}
示例12: Index_Privileged_IncludesNotForSaleGame
public async void Index_Privileged_IncludesNotForSaleGame(string role)
{
futureGame1.GameAvailabilityStatus = AvailabilityStatus.NotForSale;
futureGame2.GameAvailabilityStatus = AvailabilityStatus.PreOrder;
pastGame1.GameAvailabilityStatus = AvailabilityStatus.NotForSale;
pastGame2.GameAvailabilityStatus = AvailabilityStatus.Available;
pastGame3.GameAvailabilityStatus = AvailabilityStatus.DiscontinuedByManufacturer;
List<Game> games = new List<Game>
{
futureGame1, futureGame2, pastGame1, pastGame2, pastGame3
};
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(games.AsQueryable());
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().IsInRole(role);
HomeController controller = new HomeController(dbStub.Object)
{
ControllerContext = contextStub.Object
};
var result = await controller.Index() as ViewResult;
Assert.That(result != null);
Assert.That(result.Model, Is.InstanceOf<HomePageViewModel>());
var model = (HomePageViewModel)result.Model;
Assert.That(model.ComingSoon, Has.Count.EqualTo(2));
Assert.That(model.ComingSoon, Has.Member(futureGame1));
Assert.That(model.ComingSoon, Has.Member(futureGame2));
Assert.That(model.NewReleases, Has.Count.EqualTo(3));
Assert.That(model.NewReleases, Has.Member(pastGame1));
Assert.That(model.NewReleases, Has.Member(pastGame2));
Assert.That(model.NewReleases, Has.Member(pastGame3));
}
示例13: AdvancedSearch_TitleMatchingAll_OrdersResultByName
public async void AdvancedSearch_TitleMatchingAll_OrdersResultByName()
{
List<Game> games = GetGameSearchList();
List<Platform> platforms = GetPlatformList();
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(games.AsQueryable());
Mock<DbSet<Platform>> platformDbSetStub = TestHelpers.GetFakeAsyncDbSet(platforms.AsQueryable());
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
dbStub.Setup(db => db.Platforms).Returns(platformDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().InAllRoles();
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
var result = await controller.AdvancedSearch(null, title: TITLE_FRAGMENT_COMMON_TO_ALL_SEARCH_GAMES) as ViewResult;
Assert.That(result != null);
Assert.That(result.Model, Is.InstanceOf<GameListViewModel>());
var model = (GameListViewModel)result.Model;
Assert.That(games, Is.Not.Ordered.By(nameof(Game.Name)), "Games must not be ordered for this test to be valid");
Assert.That(model.Games, Is.Ordered.By(nameof(Game.Name)));
}
示例14: AdvancedSearch_KeywordIsPartOfNotForSaleTitle_Employee_ReturnsThatGame
public async void AdvancedSearch_KeywordIsPartOfNotForSaleTitle_Employee_ReturnsThatGame(string role)
{
List<Game> games = GetGameSearchList();
List<Platform> platforms = GetPlatformList();
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(games.AsQueryable());
Mock<DbSet<Platform>> platformDbSetStub = TestHelpers.GetFakeAsyncDbSet(platforms.AsQueryable());
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
dbStub.Setup(db => db.Platforms).Returns(platformDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().IsInRole(role);
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
var result = await controller.AdvancedSearch(null, title: "NotForSale") as ViewResult;
Assert.That(result != null);
Assert.That(result.Model, Is.InstanceOf<GameListViewModel>());
var model = (GameListViewModel)result.Model;
Assert.That(model.Games, Has.Count.EqualTo(1));
}
示例15: AdvancedSearch_KeywordIsPartOfTitle_ReturnsIEnumerableOfMatchingGames
public async void AdvancedSearch_KeywordIsPartOfTitle_ReturnsIEnumerableOfMatchingGames()
{
List<Game> games = GetGameSearchList();
List<Platform> platforms = GetPlatformList();
Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
Mock<DbSet<Game>> gameDbSetStub = TestHelpers.GetFakeAsyncDbSet(games.AsQueryable());
Mock<DbSet<Platform>> platformDbSetStub = TestHelpers.GetFakeAsyncDbSet(platforms.AsQueryable());
dbStub.Setup(db => db.Games).Returns(gameDbSetStub.Object);
dbStub.Setup(db => db.Platforms).Returns(platformDbSetStub.Object);
Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
contextStub.SetupUser().InAllRoles();
GamesController controller = new GamesController(dbStub.Object, idGetter: null)
{
ControllerContext = contextStub.Object
};
var result = await controller.AdvancedSearch(null, title: TITLE_FRAGMENT_COMMON_TO_ALL_SEARCH_GAMES) as ViewResult;
Assert.That(result != null);
Assert.That(result.Model, Is.InstanceOf<GameListViewModel>());
var model = (GameListViewModel)result.Model;
Assert.That(model.Games, Has.Count.EqualTo(games.Count));
}