0

我試圖寫我的一個小項目的一些測試,但他們忽視了(我從1個開始測試「Create_Class」)在ASP.NET MVC3(用DI)TDD問題

我用的是庫模式,並使用構造函數依賴注入:

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與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"]); 

    } 
} 

此刻我得到一個nullreferenc在視圖eException(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)); 
} 

的代碼,我知道這是一個很大的代碼,但我真的想使這項工作。

謝謝你幫我提前出局:)

+2

根據您的測試方法創建操作必須返回RedirectToRouteResult類型的結果。這樣對嗎? – vladimir77

回答

2

由於vladimir77在他的評論中已經指出,該方法public ActionResult Create(int id)是類型的ViewResult的,所以要麼你改變你的方法做一個return RedirectToRoute()或者你改變你的測試

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

A ViewResult不能轉換爲RedirectToRouteResult

+0

傻我:p謝謝,現在測試通過了,我寫了一些更多(通過)測試。我現在應該很好,謝謝你的幫助! –