public void Save_ValidViewModelWithObjectStateAdd_ARecipeWasAddedToDatabase() { //Arrange var user = _context.Users.First(); _recipesControllers.MockControllerContext(user.Id, user.UserName); var recipeFormViewModel = CreateRecipeFormViewModel(ObjectState.Added, ObjectState.Added); //Action var result = _recipesControllers.Save(recipeFormViewModel); var recipe = _context.Recipes.Single(r => r.Title == "Test Recipe"); //Assert //NUnit Assertions Assert.IsNotNull(recipe); Assert.AreEqual(1, recipe.RecipeIngredients.Count); Assert.IsTrue(recipe.Title == "Test Recipe"); //Fluent Assertions recipe.Title.Should().Be("Test Recipe"); recipe.RecipeIngredients.Should().HaveCount(1); }
public void TestInitialize() { //Create a mock RecicpeRepository _mockRecipeRepository = new Mock <IRecipeRepository>(); //Create a mock UnitOfWork var mockUoW = new Mock <IUnitOfWork>(); //Get the RecipeRepository Object mockUoW.SetupGet(u => u.Recipes).Returns(_mockRecipeRepository.Object); var identity = new GenericIdentity("tugberk"); _controller = new RecipesController(mockUoW.Object); _controller.MockControllerContext("1", "*****@*****.**"); //// In the case of an ApiController we can use the Extension Method to mock the current user //_controller = new YourApiController(mockUoW); //Use the extension method created //_controller.MockCurrentUserForApiControllers("1", "userName"); }