title: Write Moq Unit Test for internal method in the same class using Autofac
body: <p>I am trying mock internal method in the same class.But my mocking fails.</p>
  
  <p>Here is my code.</p>
  
  <p>Interface</p>
  
  <pre><code>public interface IStudentService
  {
      int GetRank(int studentId);
      IList&lt;Subject&gt; GetSubjects(int studentId);
  }
  </code></pre>
  
  <p>Implementation </p>
  
  <pre><code>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&lt;Subject&gt; subjects = GetSubjects(studentId);
  
          int rank = 0;
          //
          //Calculate Rank
          //
          return rank;
      }
  
      public virtual IList&lt;Subject&gt; GetSubjects(int studentId)
      {
          return _subjectRepository.GetAll(studentId);
      }
  }
  </code></pre>
  
  <p>Unit Test</p>
  
  <pre><code>[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&lt;IStudentService&gt;();
              mockStudentService.Setup(x =&gt; x.GetSubjects(1)).Returns(new ServiceResponse&lt;SystemUser&gt;(new List&lt;Subject&gt;{ new AccounProfile(), new AccounProfile()}));
              mock.Provide(mockStudentService.Object);
  
              var component = mock.Create&lt;StudentService&gt;();
              int rank = component.GetRank(1);
              mockStudentService.VerifyAll();
  
              Assert.AreEqual(1, rank, "GetRank method fails");
          }
      }
  }
  </code></pre>
  
  <p>When I debug the code it is not mocking the GetSubjects method. It actually go inside to that method. I am using Nunit, Moq and Autofac to write unit test.</p>
  
  <p>Thanks In Advance! </p>