使用Autofac在同一类中为内部方法编写Moq单元测试

时间:2014-11-03 11:31:48

标签: c# unit-testing nunit moq autofac

我在同一个类中尝试模拟内部方法。但是我的模拟失败了。

这是我的代码。

接口

public interface IStudentService
{
    int GetRank(int studentId);
    IList<Subject> GetSubjects(int studentId);
}

实施

public class StudentService : IStudentService
{
    private readonly IStudentRepository _studentRepository;
    private readonly ISubjectRepository _subjectRepository;

    public StudentService(IStudentRepository studentRepository, ISubjectRepository subjectRepository)
    {
        _studentRepository = studentRepository;
        _subjectRepository = subjectRepository;
    }

    public int GetRank(int studentId)
    {
        IList<Subject> subjects = GetSubjects(studentId);

        int rank = 0;
        //
        //Calculate Rank
        //
        return rank;
    }

    public virtual IList<Subject> GetSubjects(int studentId)
    {
        return _subjectRepository.GetAll(studentId);
    }
}

单元测试

[TestFixture]
public class StudentServiceTest
{
    [SetUp]
    public void Setup()
    {

    }

    [TearDown]
    public void TearDown()
    {

    }

    [Test]
    public void GetRankTest()
    {
        using (var mock = AutoMock.GetStrict())
        {
            var mockStudentService = new Mock<IStudentService>();
            mockStudentService.Setup(x => x.GetSubjects(1)).Returns(new ServiceResponse<SystemUser>(new List<Subject>{ new AccounProfile(), new AccounProfile()}));
            mock.Provide(mockStudentService.Object);

            var component = mock.Create<StudentService>();
            int rank = component.GetRank(1);
            mockStudentService.VerifyAll();

            Assert.AreEqual(1, rank, "GetRank method fails");
        }
    }
}

当我调试代码时,它并没有模仿GetSubjects方法。它实际上进入了那个方法。我正在使用Nunit,Moq和Autofac编写单元测试。

提前致谢!

3 个答案:

答案 0 :(得分:4)

有两种解决方案。

1。部分嘲笑

在这种方法中,你创建了你正在测试的组件的模拟(StudentService)并告诉Moq模拟它的一些方法(GetSubjects - to-be-mocked 方法必须是虚拟的),同时将其他人(GetRank)委托给base implementation

  

设置mock.CallBase = true会指示Moq将与显式Setup调用不匹配的任何调用委托给其基本实现。

// mockStudentService is not needed, we use partial mock
var service = mock.Create<StudentService>();
service.CallBase = true;
service.Setup(m => m.GetSubjects(1)).Returns(...);

var rank = service.GetRank(1);
// you don't need .VerifyAll call, you didn't not set any expectations on mock
Assert.AreEqual(1, rank, "GetRank method fails");

2。模拟内部服务(ISubjectRepository

部分嘲讽是为特殊情况保留的。你的情况很常见。您的组件(StudentService)可以依靠模拟ISubjectRepository为其提供主题,而不是模仿自身:

using (var mock = AutoMock.GetStrict())
{
    var subjectRepositoryMock = new Mock<ISubjectRepository>();
    subjectRepositoryMock.Setup(x => x.GetSubjects(1)).Returns(...);
    mock.Provide(subjectRepositoryMock.Object);

    var component = mock.Create<StudentService>();
    int rank = component.GetRank(1);
    // verify is not needed once again

    Assert.AreEqual(1, rank, "GetRank method fails");
}

答案 1 :(得分:0)

我猜你的GetSubjects方法必须声明为虚拟,否则无法模拟。

public virtual IList<Subject> GetSubjects(int studentId)
{
   // code here
} 

答案 2 :(得分:0)

此代码适用于。感谢大家的支持

[TestFixture]
public class StudentServiceTest
{
    private Mock<StudentRepository> _studentRepositoryMock;
    private Mock<SubjectRepository> _subjectRepositoryMock;
    private Mock<StudentService> _studentServiceMock;

    [SetUp]
    public void Setup()
    {
        _studentRepositoryMock = new Mock<StudentService>(MockBehavior.Strict);
        _subjectRepositoryMock = new Mock<SubjectRepository>(MockBehavior.Strict);
        _studentServiceMock = new Mock<StudentService>(_studentRepositoryMock.Object, _subjectRepositoryMock.Object);
        _studentServiceMock.CallBase = true;
    }

    [TearDown]
    public void TearDown()
    {

    }

    [Test]
    public void GetRankTest()
    {
        _studentServiceMock.Setup(x => x.GetSubjects(1)).Returns(...);

        int rank = component.GetRank(1);
        _studentServiceMock.VerifyAll();

        Assert.AreEqual(1, rank, "GetRank method fails");
    }
}