对于单元测试如何与我当前的项目设计联系起来,我有点迷茫。我目前的测试都是集成测试,最终几乎只是测试第三方 ORM。我在想我需要使用存储库模式,但我不清楚如何将它作为一个层添加到我的应用程序中。这是我正在使用的一个基本对象作为示例:
public class Venue : ModelBase<Venue>
{
public Venue ()
: base()
{
}
public virtual string VenueName { get; set; }
}
及其行动:
[HttpGet]
public ActionResult Create ()
{
return View();
}
public ActionResult Create ([ModelBinder(typeof(VenueBinder))]Venue Venue)
{
Venue.Save();
return RedirectToAction("List", "Venue");
}
[HttpGet]
public ActionResult Edit (long id)
{
return View(Venue.Load(id));
}
[HttpPost]
public ActionResult Edit ([ModelBinder(typeof(VenueBinder))]Venue v, long id)
{
VenueBinder.EditModel(v, id);
return RedirectToAction("List", "Venue");
}
public ActionResult Delete (long id)
{
Venue temp = Venue.Load(id);
var eventController = new EventController();
foreach (var e in Event.All().ToList().FindAll(x => x.Venue.ID == id))
{
eventController.Delete(e.ID);
}
temp.Delete();
return RedirectToAction("List", "Venue");
}
和模型粘合剂:
public class VenueBinder : IModelBinder
{
/// <summary>
/// bind form data to Venue model
/// </summary>
public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext)
{
return new Venue
{
VenueName = bindingContext.ValueProvider.GetValue("name").AttemptedValue
};
}
/// <summary>
/// edits an existing Venue object
/// </summary>
public static void EditModel (Venue v, long id)
{
Venue temp = Venue.Load(id);
temp.VenueName = v.VenueName;
temp.Save();
}
}