Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6372141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:11:49+00:00 2026-05-25T01:11:49+00:00

I am attempting to write some Tests for a small project of mine but

  • 0

I am attempting to write some Tests for a small project of mine but they seem to fail (I am starting with 1 test ‘Create_Class’)

I use the repository pattern and use 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;
}

here is my TestClass with testinitializer (which runs before every test) and the first test

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

    }
}

at the moment I get a nullreferenceException on the view (assert.isNotNull fails)

and here is one of my DummyRepository’s:

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();
    }
}

Normally I should have written the tests Before writting the code, I know (and I am convinced off TDD, as I have used it successfully in my latest Java-project). but it just doesn’t seem to work..

here is the code for KlasController.Create action

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));
} 

I know this is a lot of code, but I really want to make this work.

Thanks for helping me out in advance 🙂

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:11:50+00:00Added an answer on May 25, 2026 at 1:11 am

    As vladimir77 already says in his comment, the method public ActionResult Create(int id) is of type ViewResult, so either you change you method to do areturn RedirectToRoute() or you change your test to

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

    A ViewResult can not be cast as a RedirectToRouteResult.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to write some tests using webtest to test out my python
I'm attempting to write some unit tests using EasyMock and TestNG and have run
I am attempting to write some JUnit tests for my android application. The application
I'm attempting to write some tests using the built-in android Junit testing framework. I
I'm trying to write some small tests for a fairly small part of a
I am attempting to write some unit tests for some Ada code I recently
I'm attempting to write some annotations to help associatate test case methods with various
I am attempting to use PHPunit to mock out some dependencies, but my mock
I'm attempting to use JUnit to test some database code. I'm pretty new to
I'm attempting to write a mod_rewrite rule to remap some friendly URLs for searches,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.