Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq framework Func<T,T>

I am new with Moq and TDD and what I am trying to do is to set up method on repository interface.

Here is full story.

I have a domain entity class called Tenant with property BusinessIdentificationNumber

public class Tenant:EntityBase<Tenant>,IAggregateRoot
{
 ...
 public string BusinessIdentificationNumber {get;set;}
 ...
}

Next I have repository for this entity which interface is like

public interface IRepository<T>
{
 ...
 T FindBy(Func<T,bool> func);
 ...
}

where the problem is, I use a domain service which holds the rules for creating a tenant and is like

public class TenantCreationService:ITenantCreationService
{
    public TenantCreationService(IRepository<Tenant> tenantRepository){...}

    public void CreateTenant(Tenant tenant)
    {
        //from here there is call to IRepository<Tenant>.FindBy(funcMethod);
    }
}

And in unit testing where I am testing the TenantCreationService I mock the repository passed to constructor, but I would like to test the feature :

  • when tenant with BusinessIdentificationNumber already exists in storage or session it should be returned.

So I was trying to do it like

repositoryMock.Setup(x=>x.FindBy(It.Is<Tenant>(t=>t.BusinessIdentificationNumber
   == _tenantInTest.BusinessIdentificationNumber))).Returns(_tenantInTest) 

but it does not compile. You know what I want to do?

EDIT: when i try to compile the snippet below

repositoryMock.Setup(e => e.FindBy(t => t.BusinessNumber == _validTenant.BusinessNumber)).Returns(
                _validTenant);

i get exception

Unsupported expression: t => (t.BusinessNumber == value(DP.IPagac.UnitTests.DP.IPagac.Module.TenantManagement.TenantDomainServiceTests)._validTenant.BusinessNumber)
like image 548
Ivan Avatar asked Feb 10 '12 12:02

Ivan


1 Answers

I think what you are trying the acheive is this (removed some things that were extraneous for example and created ITenent so it can be mocked dynamically):

[TestFixture]
public class Test
{
    [Test]
    public void CreateTenentAlreadyExistsTest()
    {
        var tenentMock = new Mock<ITenant>();
        var repoMock = new Mock<IRepository<ITenant>>();

        tenentMock.Setup(t => t.BusinessIdentificationNumber).Returns("aNumber");

        repoMock.Setup(r => r.FindBy(It.Is<System.Func<ITenant, bool>>(func1 => func1.Invoke(tenentMock.Object)))).Returns(tenentMock.Object);

        var tenantCreationService = new TenantCreationService(repoMock.Object);

        tenantCreationService.CreateTenant(tenentMock.Object);

        tenentMock.VerifyAll();
        repoMock.VerifyAll();
    }
}

public interface ITenant
{
    string BusinessIdentificationNumber { get; set; }
}

public class Tenant : ITenant
{
    public string BusinessIdentificationNumber { get; set; }
}

public interface IRepository<T>
{
    T FindBy(System.Func<T, bool> func);
}

public class TenantCreationService : ITenantCreationService
{
    private readonly IRepository<ITenant> _tenantRepository;

    public TenantCreationService(IRepository<ITenant> tenantRepository)
    {
        _tenantRepository = tenantRepository;
    }

    public void CreateTenant(ITenant tenant)
    {
        var existingTenant =
            _tenantRepository.FindBy(t => t.BusinessIdentificationNumber == tenant.BusinessIdentificationNumber);

        if (existingTenant == null)
        {
            //do stuff
        }
    }
}

public interface ITenantCreationService
{
    void CreateTenant(ITenant tenant);
}
like image 107
Myles McDonnell Avatar answered Oct 12 '22 20:10

Myles McDonnell