Mocking UnitOfWork与Moq和EF 4.1

时间:2011-07-24 21:36:46

标签: unit-testing asp.net-mvc-3 moq unit-of-work

我正在通过Contoso样本进行一些TDD练习,我检索学生的测试正在通过。

我创建新学生的测试失败了(虽然实际代码有效),因为我认为DBContext没有被嘲笑。

为了让这个测试过去,我应该重构什么?

测试失败了:

Contoso.Tests.Controllers.StudentControllerTest.Create_HttpPost_Should_Save_New_Student:   预计:9   但是:8

继承具体的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);
    }
}

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

...
}

这是我嘲笑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;
    }

这是失败的测试

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 个答案:

答案 0 :(得分:4)

看起来像StudentRepository的Add方法也需要被模拟。我不确定语法是否正确,但请尝试以下方法:

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