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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:23:17+00:00 2026-05-24T01:23:17+00:00

I am working my way through the Contoso sample for some TDD practice and

  • 0

I am working my way through the Contoso sample for some TDD practice and my tests for retrieving students are passing.

My test for creating a new student fails (although the actual code works) as I believe the DBContext is not being mocked.

What should I refactor in order to get this test to past?

The test fails as so:

Contoso.Tests.Controllers.StudentControllerTest.Create_HttpPost_Should_Save_New_Student:
Expected: 9
But was: 8

Heres the concrete UnitOfWork

public class UnitOfWork : IUnitOfWork
{
    private SchoolContext context = new SchoolContext();

    private IStudentsRepository studentsRepository;

    private bool disposed = false;

    public IStudentsRepository StudentsRepository
    {
        get
        {
            if (this.studentsRepository == null)
            {
                this.studentsRepository = new StudentsRepository(context);
            }
            return studentsRepository;
        }
    }


    public void Save()
    {
        context.SaveChanges();
    }


    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

The StudentController

public class StudentController : Controller
{
    private IUnitOfWork _unitOfWork;

    public StudentController(IUnitOfWork unitOfWork)
    {
        this._unitOfWork = unitOfWork;
    }

...

    [HttpPost]
    public ActionResult Create(Student student)
    {
        if (ModelState.IsValid)
        {
            _unitOfWork.StudentsRepository.Add(student);
            _unitOfWork.StudentsRepository.Save();

            return RedirectToAction("Details", new { id = student.StudentID });
        }

        return View(student);
    }

...
}

Here’s where I mock the IUnitOfWork

    public static IUnitOfWork MockUnitOfWork(List<Student> students)
    {
        var mockUnitOfWork = new Mock<IUnitOfWork>();

        mockUnitOfWork.Setup(x => x.StudentsRepository.Students()).Returns(students.AsQueryable());

        return mockUnitOfWork.Object;
    }

Here’s the failing test

private IUnitOfWork unitOfWork = UnitTestHelpers.MockUnitOfWork(testData);
...



    [Test]
    public void Create_HttpPost_Should_Save_New_Student()
    {
        // Arrange
        var studentsCount = unitOfWork.StudentsRepository.Students().Count();
        var controller = new StudentController(unitOfWork);
        var student = Fakes.FakeStudentsData.CreateStudent();

        // Act
        var result = controller.Create(student);

        // Assert
        Assert.AreEqual(studentsCount+1, unitOfWork.StudentsRepository.Students().Count());
        Assert.IsInstanceOf(typeof(RedirectToRouteResult), result);
    }
  • 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-24T01:23:18+00:00Added an answer on May 24, 2026 at 1:23 am

    It looks like the Add method for StudentRepository needs to be mocked as well. I’m not if sure if the syntax is right, but try the following:

    public static IUnitOfWork MockUnitOfWork(List<Student> students)
    {
        var mockUnitOfWork = new Mock<IUnitOfWork>();
    
        mockUnitOfWork.Setup(x => x.StudentsRepository.Students()).Returns(students.AsQueryable());
        mockUnitOfWork.Setup(x => x.StudentsRepository.Add(It.IsAny<Student>())).Callback<Student>((s) => students.Add(s));
    
        return mockUnitOfWork.Object;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working my way through some Objective-C code that I did not write
I am new to android development and am currently working my way through the
I am working through a code sample, and I just want to hear some
I have been working my way through Scott Guthrie's excellent post on ASP.NET MVC
I've been slowly working my way through the list of Project Euler problems, and
I recently discovered Erlang and am now working my way through a couple of
I've been learning Objective-C and Cocoa by working my way through the Hillegass book
I'm starting out with wxPython and have been working my way through every tutorial
Working my way through Effective STL at the moment. Item 5 suggests that it's
I working my way through the Apress Pro ASP.NET MVC Framework ( http://www.apress.com/book/view/9781430210078 )

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.