単体テストができるように、Moqを使用してリポジトリレイヤーをモックしています。
リポジトリレイヤーのInsertメソッドは、db挿入が成功すると、エンティティのIdプロパティを更新します。
Insertメソッドが呼び出されたときにエンティティのIdプロパティを更新するようにmoqを構成するにはどうすればよいですか?
リポジトリコード:-
void IAccountRepository.InsertAccount(AccountEntity account);
単体テスト:-
[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);
}
ContractServiceクラス(WCFサービスコントラクト)からの実装スニペット。
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
}
この情報が少し不足している可能性があることをお詫び申し上げます。私は今日、moqとモックフレームワークを学び始めました。交流