I just want to know what can be done to make this code smaller in a nice and easy way.
Tests
using System;
using Vegan.Test.TestClass;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.Entity.Validation;
using System.Linq;
namespace Vegan.Test
{
[TestClass]
public class IngredientsTest
{
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context)
{
System.Data.Entity.Database.SetInitializer(new DatabaseInitializer());
}
[TestMethod]
public void DeleteIngredient()
{
using (var ctx = new TestDbContext())
{
//Arrange
Ingredient ingredient = new TestClass.Ingredient();
ingredient.Id = 1;
ingredient.Name = "Watermelon";
ingredient.VegeOrVegetarian = VeganType.Both;
//Act
ctx.ingredients.Add(ingredient);
ctx.SaveChanges();
IngredientService service = new IngredientService(ctx);
service.removeIngredient(ingredient.Id);
service.saveChanges();
//Assert
Assert.AreEqual(0, ctx.ingredients.Count());
}
}
[TestMethod]
public void AddIngredient()
{
using (var ctx = new TestDbContext())
{
//Arrange
Ingredient ingredient = new TestClass.Ingredient();
ingredient.Id = 1;
ingredient.Name = "Watermelon";
ingredient.VegeOrVegetarian = VeganType.Both;
//Act
ctx.ingredients.Add(ingredient);
ctx.SaveChanges();
IngredientService service = new IngredientService(ctx);
var TestIngredientList = service.getIngredients();
//Assert
Assert.AreEqual(1, TestIngredientList.Count);
}
}
[TestMethod]
public void AddIngredientWithTooShortName()
{
using (var ctx = new TestDbContext())
{
//Arange
Ingredient ingredient = new Ingredient();
ingredient.Id = 1;
ingredient.Name = "";
ingredient.VegeOrVegetarian = VeganType.Both;
//Act
ctx.ingredients.Add(ingredient);
try
{
ctx.SaveChanges();
}
catch (DbEntityValidationException db)
{
foreach (var validationErrors in db.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
//Assert
Assert.Fail("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
}
}
[TestMethod]
public void AddIngredientWithNullName()
{
using (var ctx = new TestDbContext())
{
//Arrange
Ingredient ingredient = new Ingredient();
ingredient.Id = 1;
ingredient.Name = null;
ingredient.VegeOrVegetarian = VeganType.Both;
ctx.ingredients.Add(ingredient);
//act
try
{
ctx.SaveChanges();
}
catch (DbEntityValidationException db)
{
foreach (var validationErrors in db.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
//Assert
Assert.Fail("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
}
}
}
Other classes
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vegan.Test.TestClass
{
public enum VeganType
{
Vegan,
Vegetarian,
Both
};
public class TestDbContext : DbContext,IDisposable
{
public virtual IDbSet<Ingredient> ingredients { get; set; }
}
public class DatabaseInitializer : System.Data.Entity.DropCreateDatabaseAlways<TestDbContext>
{
protected override void Seed(TestDbContext context)
{
context.SaveChanges();
}
}
public class Ingredient
{
public int Id { get; set; }
[Required(ErrorMessage ="Name is required")]
[StringLength(60,ErrorMessage ="Value must be between 3 and 60",MinimumLength =3)]
public String Name { get; set; }
public VeganType VegeOrVegetarian { get; set; }
}
public class IngredientService
{
private readonly TestDbContext _TestDbContext;
public IngredientService(TestDbContext TestDbContext)
{
_TestDbContext = TestDbContext;
}
public List<Ingredient> getIngredients()
{
return _TestDbContext.ingredients.ToList();
}
public void removeIngredient(int id)
{
var toRemove = _TestDbContext.ingredients.Find(id);
_TestDbContext.ingredients.Remove(toRemove);
}
public void saveChanges()
{
_TestDbContext.SaveChanges();
}
}
}