using System; using System.Collections.Generic; using System.Linq; using System.Text; using Chefbook.DataObjects; using NUnit.Framework; using Rhino.Mocks; using Chefbook.UserActions; using Chefbook.ViewModels; namespace UnitTests.UserActionsTests { [TestFixture] public class DeleteCategoryActionFixture { private MockRepository mocks; private IUserAction action; private CategoryViewModel root, testCategory, subCategory; private RecipeViewModel recipe, recipe1, recipe2; [SetUp] public void Init() { mocks = new MockRepository(); root = new CategoryViewModel(new Category(null)) { Name = "root" }; recipe = new RecipeViewModel(new Recipe(root.CategoryModel)); testCategory = new CategoryViewModel(new Category(root.CategoryModel)) { Name = "MyCategory" }; subCategory = new CategoryViewModel(new Category(testCategory.CategoryModel)) { Name = "Sub" }; recipe1 = new RecipeViewModel(new Recipe(testCategory.CategoryModel)); recipe2 = new RecipeViewModel(new Recipe(testCategory.CategoryModel)); Assert.Contains(testCategory, recipe1.Categories); Assert.Contains(recipe1, testCategory.Recipes); Assert.Contains(testCategory, recipe2.Categories); Assert.Contains(recipe2, testCategory.Recipes); Assert.Contains(subCategory, testCategory.Subcategories); Assert.IsFalse(recipe.Categories.Contains(testCategory), "recipe.Categories.Contains(testCategory)"); //The action should never change this Assert.IsFalse(testCategory.Recipes.Contains(recipe), "testCategory.Recipes.Contains(recipe)"); Assert.True(root.Recipes.Contains(recipe), "root.Recipes.Contains(recipe)"); action = new DeleteCategoryAction(testCategory) ; } [Test] public void DoActionTest() { var parent = testCategory.Parent; action.DoAction(); Assert.IsFalse(root.Subcategories.Contains(testCategory), "Delete test failed"); Assert.IsNull(testCategory.Parent); foreach (var rec in testCategory.Recipes) { Assert.IsFalse(rec.Categories.Contains(testCategory), "Category not removed from all recipes"); } //The action should never change this Assert.IsFalse(testCategory.Recipes.Contains(recipe)); Assert.True(root.Recipes.Contains(recipe), "root.Recipes.Contains(recipe)"); } [Test] public void UndoActionTest() { var parent = testCategory.Parent; action.DoAction(); action.UndoAction(); Assert.True(parent.Subcategories.Contains(testCategory), "Delete test failed"); foreach (var rec in testCategory.Recipes) { Assert.True(rec.Categories.Contains(testCategory), "Category not removed from all recipes"); } Assert.Contains(testCategory, recipe1.Categories); Assert.Contains(recipe1, testCategory.Recipes); Assert.Contains(testCategory, recipe2.Categories); Assert.Contains(recipe2, testCategory.Recipes); Assert.Contains(subCategory, testCategory.Subcategories); Assert.IsFalse(recipe.Categories.Contains(testCategory)); //The action should never change this Assert.IsFalse(testCategory.Recipes.Contains(recipe)); Assert.True(root.Recipes.Contains(recipe), "root.Recipes.Contains(recipe)"); } [Test] public void UndoActionWithoutDoingTest() { UserActionTestsHelper.UndoActionWithoutDoingTest(action); } [Test] public void UndoActionTwiceAfterDoingTest() { UserActionTestsHelper.UndoActionTwiceAfterDoingTest(action); } [Test] public void DoActionTwiceTest() { UserActionTestsHelper.DoActionTwiceTest(action); } [Test] public void ActionNameTest() { Assert.AreEqual(string.Format("Delete Category '{0}'",testCategory), action.ToString()); } } }