1

How to write unit tests for repository layer in domain driven design architecture using moq framework? My Repository class is as following.

public class ContactRepository : Repository<Contact, int, ContactsDb>, IContactRepository
{
    public ContactRepository(IUnitOfWork unitOfWork, IObjectContextFactory objectContextFactory)
        : base(unitOfWork, objectContextFactory) { }

    public IEnumerable<Contact> FindAll()
    {
        var predicate = PredicateBuilder.True<ContactsDb>();

        //IEnumerable<ContactsDb> contacts = findContactsSummary(predicate);
        IEnumerable<ContactsDb> contacts = ObjectContextFactory.Create().Contacts
            .Include(c => c.Addresses).Include(c => c.Communication).Include(c => c.ContactEmails)
            .Include(c => c.ContactPhones).Include(c => c.Image).Where(c => !c.IsDeleted);
        foreach (ContactsDb dc in contacts)
        {
            if (dc.ContactType == ContactType.Person)
                yield return Mapper.Map<ContactsDb, Person>(dc);
            else
                yield return Mapper.Map<ContactsDb, Company>(dc);
        }
    }

In this repository class whichones are mocked? give some example on this please.

user3151024
  • 39
  • 1
  • 6

1 Answers1

0

In a repository class you are basically accessing to the database and map the results of your query to domain entities. So basically your are using code that is not yours. And you don't have to mock what's not yours. You can find an explanation of Uncle Bob here.

So, what I do in theses cases is to test the repository against a real database.

vgaltes
  • 1,150
  • 11
  • 18