ASP.NET MVC3中的TDD问题(带DI)

时间:2011-08-24 15:20:13

标签: asp.net asp.net-mvc-3 dependency-injection tdd repository-pattern

我正在尝试为我的一个小项目编写一些测试,但它们似乎失败了(我从1个测试'Create_Class'开始)

我使用存储库模式并使用Constructor Dependency Injection:

public KlasController() {

    db = ObjectContextPerHttpRequest.Context;
    KlasRepo = new KlasRepository(db);
    LesRepo = new LesRepository(db);
    OpdrachtRepo = new OpdrachtRepository(db);
}

//dependency injection constructor
public KlasController(IKlasRepository KlasRepo, ILesRepository LesRepo, 
    IOpdrachtRepository OpdrachtRepo) {

    this.KlasRepo = KlasRepo;
    this.LesRepo = LesRepo;
    this.OpdrachtRepo = OpdrachtRepo;
}

这是我的TestClass with testinitializer(在每次测试之前运行)和第一次测试

[TestClass()]
public class KlasControllerTest
{
    private KlasController Controller;
    private IOpdrachtRepository OpdrachtRepo;

    //Use TestInitialize to run code before running each test
    [TestInitialize()]
    public void MyTestInitialize()
    {
        OpdrachtRepo = new DummyOpdrachtRepository();
        Controller = new KlasController(new DummyKlasRepository(),
            new DummyLesRepository(), OpdrachtRepo);
        Opdracht TestOpdracht = new Opdracht
        {
            OpdrachtID = 1,
            VakID = 1,
            StamNummer = "im1"
        };
        Vak TestVak = new Vak { VakID = 1, VakNaam = "FOOP" };
        TestOpdracht.Vak = TestVak;
        OpdrachtRepo.addOpdracht(TestOpdracht);
    }   

    /// <summary>
    ///A test for Index
    ///</summary>
    [TestMethod()]
    public void CreateKlasDirectsToToonKlassen()
    {
        Klas Klas = new Klas { KlasNaam = "2dNet" };
        RedirectToRouteResult view = Controller.Create(1) as RedirectToRouteResult;
        Assert.IsNotNull(view);                
        Assert.AreEqual("ToonKlassen", view.RouteValues["action"]);

    }
}

目前我在视图上遇到nullreferenceException(assert.isNotNull失败)

这是我的DummyRepository之一:

class DummyOpdrachtRepository : IOpdrachtRepository
{
    List<Opdracht> opdrachten; 

    public DummyOpdrachtRepository()
    {
        opdrachten = new List<Opdracht>();
    }

    public void addOpdracht(Opdracht opdracht)
    {
        opdrachten.Add(opdracht);
    }

    public string GetDocentID(int OpdrachtID)
    {
        var opdracht = opdrachten.Where(o => o.OpdrachtID == OpdrachtID).FirstOrDefault();
        return opdracht.StamNummer;
    }

    public Opdracht Find(int id)
    {
        return opdrachten.Where(o => o.OpdrachtID == id).FirstOrDefault();
    }
}

通常我应该编写测试在编写代码之前,我知道(我确信TDD,因为我已经在我最新的Java项目中成功使用了它)。但它似乎没有用..

这是KlasController.Create操作的代码

public ActionResult Create(int id) //id = opdrachtID 
{
    var Opdracht = OpdrachtRepo.Find(id);
    Vak vak;
    if(Opdracht != null)
        vak = Opdracht.Vak;
    else
        throw new NullReferenceException("Deze opdracht werd niet gevonden");
    return View(new CreateKlasModel(id,vak));
} 

我知道这是很多代码,但我真的想让这个工作。

感谢您提前帮助我:)

1 个答案:

答案 0 :(得分:2)

正如vladimir77在评论中已经说过,方法public ActionResult Create(int id)的类型为ViewResult,因此要么改变方法来执行return RedirectToRoute(),要么将测试更改为

ViewResult view = Controller.Create(1);
Assert.IsNotNull(view); 

ViewResult无法转换为RedirectToRouteResult