Moq Func <T, T>

I am new to Moq and TDD, and I am trying to configure a method in the repository interface.

Here is the full story.

I have a domain entity class called Tenant with the BusinessIdentificationNumber property

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

Next, I have a repository for this entity, whose interface is similar to

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

where is the problem, I use a domain service that contains rules for creating a tenant and looks 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 test TenantCreationService, I make fun of the repository passed to the constructor, but I would like to test this function:

  • when a tenant with a BusinessIdentificationNumber already exists in the store or session, it must be returned.

So, I tried to do it like

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

but it does not compile. Do 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 an exception

Unsupported expression: t => (t.BusinessNumber == value(DP.IPagac.UnitTests.DP.IPagac.Module.TenantManagement.TenantDomainServiceTests)._validTenant.BusinessNumber)
+5
2

, ( , , , ITenent, ):

[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);
}
+6

", BusinessIdentificationNumber , ." - ,

unit test , , , FindBy . ITenantRepositry, IRepository Repostiry.

0

All Articles