请问关于c的具体应用场景有哪些?

2026-04-29 02:521阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计374个文字,预计阅读时间需要2分钟。

请问关于c的具体应用场景有哪些?

我正在使用Moq来模拟我的Repository层,以便进行单元测试。我的存储库层在生成成功的数据库插入时更新我的实体的Id属性。在调用Insert方法时,如何配置Moq以更新实体的Id属性?

我正在使用Moq来模拟我的Repository层,所以我可以进行单元测试.

我的存储库层插入方法在发生成功的数据库插入时更新我的​​实体的Id属性.

在调用Insert方法时,如何配置moq以更新实体的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 }

如果这些信息可能有点缺乏,我深表歉意.我今天才开始学习moq和模拟框架. AC

您可以使用Callback方法来模拟副作用.就像是:

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

这是未经测试的,但它是沿着正确的路线.

请问关于c的具体应用场景有哪些?

本文共计374个文字,预计阅读时间需要2分钟。

请问关于c的具体应用场景有哪些?

我正在使用Moq来模拟我的Repository层,以便进行单元测试。我的存储库层在生成成功的数据库插入时更新我的实体的Id属性。在调用Insert方法时,如何配置Moq以更新实体的Id属性?

我正在使用Moq来模拟我的Repository层,所以我可以进行单元测试.

我的存储库层插入方法在发生成功的数据库插入时更新我的​​实体的Id属性.

在调用Insert方法时,如何配置moq以更新实体的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 }

如果这些信息可能有点缺乏,我深表歉意.我今天才开始学习moq和模拟框架. AC

您可以使用Callback方法来模拟副作用.就像是:

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

这是未经测试的,但它是沿着正确的路线.

请问关于c的具体应用场景有哪些?