title: Mock method of system-under-test with Moq
body: <p>I have the following three methods in the CompanyApplication class (along with the supporting factories and services listed):</p>
  
  <pre><code>public ResultSet&lt;CompanyDto&gt; AddCompany(CompanyDto companyDto)
  {
      var result = new CompanyDto();
      var company = new Company();
      Mapper.Map(companyDto, company);
  
      using (ITransaction t = _transactionFactory.Create())
      {
          company = _companyService.Add(company);
          t.Commit();
      }
  
      Mapper.Map(company, result);
      return new ResultSet&lt;CompanyDto&gt;(1, new[] { result });
  }
  
  public ResultSet&lt;CompanyContactDto&gt; AddCompanyContact(CompanyContactDto companyContactDto)
  {
      var result = new CompanyContactDto();
      var company = new Company();
      var contact = new CompanyContact();
      Mapper.Map(companyContactDto, contact);
  
      using (ITransaction t = _transactionFactory.Create())
      {
          var contactCompanies = FindByIdJoin&lt;Company, CompanyDto&gt;(companyContactDto.CompanySK);
          Mapper.Map(contactCompanies.Data.First(), company);
          company.CompanyContacts.Add(contact);
          company = _companyService.Update(company);
          t.Commit();
      }
  
      Mapper.Map(contact, result);
      return new ResultSet&lt;CompanyContactDto&gt;(1, new[] { result });
  }
  
  public ResultSet&lt;T_DtoType&gt; FindByIdJoin&lt;T_DbType, T_DtoType&gt;(long id)
  {
      IAbstractRepository&lt;T_DbType&gt; repository = EnsureRepository&lt;T_DbType&gt;();
      T_DbType entity = repository.FindByIdJoin(id);
      return (entity == null ? null : MapResultSetToDto&lt;T_DbType, T_DtoType&gt;(entity));
  }
  </code></pre>
  
  <p>There are other objects in play here, which is why the <code>FindByIdJoin</code> has been made a separate method in the CompanyApplication class.</p>
  
  <p>I have set up the testing class with some mocks and an instance of the CompanyApplication class:</p>
  
  <pre><code>private Mock&lt;ICompanyRepository&gt; _mockCompanyRepository;
  private Mock&lt;ICompanyDomain&gt; _mockCompanyService;
  private Mock&lt;ITransactionFactory&gt; _mockTransactionFactory;
  private Mock&lt;ITransaction&gt; _mockTransaction;
  private CompanyApplication _companyApplication;
  
  [Setup]
  public void SetUp()
  {
      _mockCompanyRepository = new Mock&lt;ICompanyRepository&gt;(MockBehavior.Strict);
      _mockCompanyService = new Mock&lt;ICompanyDomain&gt;(MockBehavior.Strict);
      _mockTransactionFactory = new Mock&lt;ITransactionFactory&gt;(MockBehavior.Strict);
      _mockTransaction = new Mock&lt;ITransaction&gt;(MockBehavior.Strict);
  
      _companyApplication = new CompanyApplication(
          _mockCompanyRepository.Object,
          _mockCompanyService.Object,
          _mockTransactionFactory.Object);
  }
  </code></pre>
  
  <p>I am successfully able to test the <code>FindByIdJoin</code> and <code>AddCompany</code> methods directly in Moq like this:</p>
  
  <pre><code>[Test]
  public void CanFindCompanyByIdJoin()
  {
      var data = new Company {ObjectId = 1, Name = "Company1"};
      _mockCompanyRepository.Setup(x =&gt; x.FindByIdJoin(It.Is&lt;long&gt;(arg =&gt; arg == data.ObjectId)))
          .Returns(data);
  
      var result = _companyApplication.FindByIdJoin&lt;Company, CompanyDto&gt;(data.ObjectId);
  
      Assert.AreEqual(data.ObjectId, result.Data.First().ObjectId);
  }
  
  [Test]
  public void CanAddCompany()
  {
      var data = new Company {ObjectId = 1, Name = "Company1"};
      _mockCompanyService.Setup(x =&gt; x.Add(It.Is&lt;Company&gt;(arg =&gt; arg.ObjectId == data.ObjectId)))
          .Returns(data);
  
      _mockTransactionFactory.Setup(x =&gt; x.Create()).Returns(_mockTransaction.Object);
      _mockTransaction.Setup(x =&gt; x.Commit());
      _mockTransaction.Setup(x =&gt; x.Dispose());
  
      var dto = new CompanyDto {ObjectId = 1, Name = "Company1"};
      var result = _companyApplication.AddCompany(dto);
  
      _mockCompanyService.Verify(t =&gt; t.Add(It.IsAny&lt;Company&gt;()));
  }
  </code></pre>
  
  <p>Those two tests pass just fine. However, I'm having trouble coming up with a test for <code>AddCompanyContact</code>, because it calls <code>FindByIdJoin</code> as part of its flow, and that seems to be getting in the way.</p>
  
  <p>Specifically, is there a way to mock <code>var contactCompanies = FindByIdJoin&lt;Company, CompanyDto&gt;(companyContactDto.CompanySK)</code> in a test for the <code>AddCompanyContact</code> method?</p>
  
  <p>Thanks!</p>