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

  • Home
  • SEARCH
  • 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 4381150
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:36:50+00:00 2026-05-21T12:36:50+00:00

Coders, I am in process of writing test cases for Asp.net MVC 3 project

  • 0

Coders, I am in process of writing test cases for Asp.net MVC 3 project and I need call an action method that insert data into a database using Entity Framework. Here is the code for the action method:

//
    // POST: /School/Create

    [HttpPost]
    public ActionResult Create(School school)
    {
        if (ModelState.IsValid)
        {
            db.Schools.Add(school);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        return View(school);
    }

And here is the code for my test method:

 [TestMethod]
    public void CreateNewSchool()
    {
        var schoolController = new SchoolController();
        var viewResult = schoolController.Index();

        //creating a school object
        School school = new School();
        school.Name = "OOO";

        //passing the school object to the action method
        schoolController.Create(school);

        //making sure that the model is not null
        Assert.IsNotNull(viewResult.Model);
    }

Notice, however, that I don’t check if the data were actually inserted in the database. I just check that the model of the view is not null. I do manually check the database using SQL server management studio.

The problem is though that when I call the action method in the test method to create/insert a record in the database nothing happened to the database. However, if I run the application and brows to the create page and try to create a new recorded then the record will be added to the database. So it appears to me that insertion to database happens only if I run the application and actually brows to the create page and hit the create button, but I cannot programmatically call the action method in the test method to insert a new record in the database. I have also debugged the test case and it did hit the db.SaveChanges(); line in the action method, but no changes were reflected in the database.

So, can someone explain to me why I am not able to insert a record by calling the action method in my test method?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 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-21T12:36:51+00:00Added an answer on May 21, 2026 at 12:36 pm

    I would look into how your db context is getting instantiated. In many cases, it is not desirable for unit tests to cause database round-trips, so people use strategies like mocking to prevent it. It might be something as simple as using a different connection string when you are running a unit test versus running it as an asp.net application.

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

Sidebar

Related Questions

We're in the process of writing an app that has 4 tabs: Map, People,
I need to change the way MVC is rendering action links(and form's and url's,
I have a process that I need to follow, I hope this makes sense.
I have noticed that most Objective-C coders never use the self->ivar syntax when accessing
I need a bit of help from any of the coders out there..... Currently,
Greetings, fellow coders! I have this table that contains categories and subcategories (actually I
I have a list of rather meaningless codes that I'm processing with a VB.NET
I am writing a utility in Java that reads a stream which may contain
I am writing a program that counts the number of words in a text
I am maintaining a project which uses inter-process COM with C++. At the top

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.