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 8228949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:38:01+00:00 2026-06-07T16:38:01+00:00

How do I unit test that the journal entry was created correctly (added to

  • 0

How do I unit test that the journal entry was created correctly (added to the db with the correct values)?

    [HttpPost]
    public ActionResult Create(
        int journalId, 
        string text)
    {
        JournalEntry journalentry = new JournalEntry();
        journalentry.Text = text;
        if (ModelState.IsValid)
        {
            var journal = db.Journals.Find(journalId);               
            journalentry.Journal = journal;                
            db.JournalEntries.Add(journalentry);
            db.SaveChanges();
        }

        return View(journalentry);
    }
  • 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-06-07T16:38:02+00:00Added an answer on June 7, 2026 at 4:38 pm

    You should not unit test against a real DbContext instance.

    Your unit tests should instead mockup interfaces and inject them into the controller.

    For example:

    public class JournalsController : Controller
    {
        private readonly IJournalsRespository _journalsDb;
    
        public JournalsController(IJournalsRepository journalsDb)
        {
            _journalsDb = journalsDb
        }
    
        [HttpPost]
        public ActionResult Create(int journalId, string text)
        {
            JournalEntry journalentry = new JournalEntry();
            journalentry.Text = text;
            if (ModelState.IsValid)
            {
                var journal = _journalsDb.FindJournal(journalId);               
                journalentry.Journal = journal;                
                _journalsDb.Add(journalentry);
                _journalsDb.SaveChanges();
            }
            return View(journalentry);
        }
    }
    

    Your interface would look something like this:

    public interface IJournalsRepository
    {
        Journal FindJournal(int id);
        void Add(JournalEntry journalEntry);
        void SaveChanges();
    }
    

    You could unit test this like so (using Moq):

    public void Create()
    {
        var db = new Mock<IJournalsRepository>();
        db.Setup(m => m.FindJournal(7)).Returns(new Journal());
        var controller = new JournalsController(db.Object);
        controller.Create(7, "test");
        db.Verify(m => m.FindJournal(7), Times.Once());
        db.Verify(m => m.Add(It.IsAny<JournalEntry>()), Times.Once());
        db.Verify(m => m.SaveChanges(), Times.Once());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a unit test that tests interactions on my ViewModel class in a
I need to create a unit test for a method that returns an xmldocument.
I have a unit-test that tests a variety of cases, like this: public void
I am trying to create a unit test that makes sure all of my
I'm writing a unit test for a method that packs boolean values into a
I am trying to create a unit test that generates an empty test document
I want to write a unit test for the following controller action: public ActionResult
For every entity that I create I write a unit test that just loads
I have a unit test that fails because headers are already sent. However, the
I have a unit test that I am writing and have run into an

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.