题
我使用的最低采购量来嘲笑我的储存库层所以我可以单元的测试。
我的储存库层插入的方法更新Id的财产我的实体时的一个成功的db插入的发生。
我该如何配置最低采购量更新Id属的实体当中插入方法被称为?
库码:-
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
}
我道歉如果这种信息可能会有一点点缺乏。我才开始学习的最低采购量和模拟框架的今天。交流
解决方案
你可以使用回调的方法来嘲笑的副作用。是这样的:
accountRepository
.Expect(r => r.InsertAccount(account))
.Callback(() => account.ID = 1);
这是未经测试,但这是沿着正确的线。
其他提示
我不知道该如何将工作,因为帐户内部创建的方法,所以它不是实例,我将可以参考的时候我设置的ID值为1。
也许有一个缺陷在我的设计和我应该检查ID>0内iaccountrepository实例的对象.InsertAccount执行情况,然后返回布尔,如果它确定。虽然然后我的一个问题插入一个账户,有一个相关的目的需要insterted(和身份证genereated).
我发现这是回答我的问题
accountRepository
.Expect(p => p.InsertAccount(It.Is<AccountEntity>(x => x.Id == null)))
.Callback<AccountEntity>(a => a.Id = 1);
谢谢。
不隶属于 StackOverflow