Question

I am using Moq to mock my Repository layer so I can unit test.

My repository layer Insert methods update the Id property of my entities when a successful db insert occurs.

How do I configure moq to update the Id property of the entity when the Insert method is called?

Repository code:-

void IAccountRepository.InsertAccount(AccountEntity account);

Unit Test:-

[TestInitialize()]
public void MyTestInitialize() 
{
    accountRepository = new Mock<IAccountRepository>();
    contactRepository = new Mock<IContactRepository>();
    contractRepository = new Mock<IContractRepository>();
    planRepository = new Mock<IPlanRepository>();
    generator = new Mock<NumberGenerator>();

    service = new ContractService(contractRepository.Object, accountRepository.Object, 
                    planRepository.Object, contactRepository.Object, generator.Object);   
}


[TestMethod]
public void SubmitNewContractTest()
{
    // Setup Mock Objects
    planRepository
        .Expect(p => p.GetPlan(1))
        .Returns(new PlanEntity() { Id = 1 });

    generator
        .Expect(p => p.GenerateAccountNumber())
        .Returns("AC0001");

    // Not sure what to do here? 
    // How to mock updating the Id field for Inserts?
    //                                                 
    // Creates a correctly populated NewContractRequest instance
    NewContractRequest request = CreateNewContractRequestFullyPopulated();

    NewContractResponse response = service.SubmitNewContract(request);
    Assert.IsTrue(response.IsSuccessful);
}

implementation snippet from ContractService class (WCF service contract).

AccountEntity account = new AccountEntity()
{
    AccountName = request.Contact.Name,
    AccountNumber = accountNumber,
    BillingMethod = BillingMethod.CreditCard,
    IsInvoiceRoot = true,
    BillingAddressType = BillingAddressType.Postal,
    ContactId = request.Contact.Id.Value
};

accountRepository.InsertAccount(account);
if (account.Id == null)
{
    // ERROR
}

I apologise if this information may be a little lacking. I only started learning moq and mocking frameworks today. ac

Was it helpful?

Solution

You can use the Callback method to mock side-effects. Something like:

accountRepository
    .Expect(r => r.InsertAccount(account))
    .Callback(() => account.ID = 1);

That's untested but it's along the right lines.

OTHER TIPS

I'm not sure how that will work because account is created inside the method, so it's not the instance I'll be referring to when I set the value of ID to 1.

Perhaps there's a flaw in my design and I should be checking for ID > 0 inside the IAccountRepository.InsertAccount implementation and then returning a bool if it's ok. Though then I've a problem with inserting an account that has a related object that needs to be insterted (and an Id genereated).

I found this to be the answer to my problem

accountRepository
    .Expect(p => p.InsertAccount(It.Is<AccountEntity>(x => x.Id == null)))
    .Callback<AccountEntity>(a => a.Id = 1);

thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top