本文介绍了如何嘲笑的DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我想测试
公共DocumentDto SaveDocument(DocumentDto documentDto)$代码b $ b {
文档文件= NULL;
使用(_documentRepository.DbContext.BeginTransaction())
{
试
{
如果(documentDto.IsDirty)
{
如果( documentDto.Id == 0)
{
=文件CreateNewDocument(documentDto);
}
,否则如果(documentDto.Id大于0)
{
文档= ChangeExistingDocument(documentDto);
}
=文件_documentRepository.SaveOrUpdate(文件);
_documentRepository.DbContext.CommitChanges();
}
}
抓
{
_documentRepository.DbContext.RollbackTransaction();
抛出;
}
}
返回MapperFactory.GetDocumentDto(文件);
}
和这里是我的测试代码
[测试]
公共无效SaveDocumentsWithNewDocumentWillReturnTheSame()
{
//安排
IDocumentService documentService =新DocumentService(_ducumentMockRepository,
_identityOfSealMockRepository,_customsOfficeOfTransitMockRepository,
_accountMockRepository,_documentGuaranteeMockRepository,
_guaranteeMockRepository,_goodsPositionMockRepository);
变种documentDto =新NctsDepartureNoDto();
//法案
VAR retDocumentDto = documentService.SaveDocument(documentDto);
//断言
Assert.AreEqual(documentDto,documentDto);
}
在我运行测试,我得到了的DbContext就行
使用(_documentRepository.DbContext.BeginTransaction())
我的问题是我不能够访问的DbContext。我将如何去解决它
解决方案
据我了解,你是通过文件服务的构造函数注入储备库 ducumentMockRepository
。
所以你可以设置这个模拟你想要的任何预期。
有关你的情况,你已经通过模拟以及
$ B替代的DbContext$ b
//我希望你有抽象的DbContext的接口?
VAR dbContextMock = MockRepository.GenerateMock< IDbContext>();
//设置预期的DbContext模拟
dbContextMock.Expect(...)的的DbContext的
//绑定到模拟repository.DbContext $ B财产$ b ducumentMockRepository.Expect(模拟= GT; mock.DbContext)
.Return(dbContextMock)
.Repeat()
。任何();
Here is the code I want to test
public DocumentDto SaveDocument(DocumentDto documentDto)
{
Document document = null;
using (_documentRepository.DbContext.BeginTransaction())
{
try
{
if (documentDto.IsDirty)
{
if (documentDto.Id == 0)
{
document = CreateNewDocument(documentDto);
}
else if (documentDto.Id > 0)
{
document = ChangeExistingDocument(documentDto);
}
document = _documentRepository.SaveOrUpdate(document);
_documentRepository.DbContext.CommitChanges();
}
}
catch
{
_documentRepository.DbContext.RollbackTransaction();
throw;
}
}
return MapperFactory.GetDocumentDto(document);
}
And here is my test code
[Test]
public void SaveDocumentsWithNewDocumentWillReturnTheSame()
{
//Arrange
IDocumentService documentService = new DocumentService(_ducumentMockRepository,
_identityOfSealMockRepository, _customsOfficeOfTransitMockRepository,
_accountMockRepository, _documentGuaranteeMockRepository,
_guaranteeMockRepository, _goodsPositionMockRepository);
var documentDto = new NctsDepartureNoDto();
//Act
var retDocumentDto = documentService.SaveDocument(documentDto);
//Assert
Assert.AreEqual(documentDto, documentDto);
}
Once I run the test I get Null exception for the DbContext on the line
using (_documentRepository.DbContext.BeginTransaction())
The problem I have is I don't have access to the DbContext. How would I go about solving it
解决方案
As far as I understand you are injecting the repository through the constructor of Document Service as ducumentMockRepository
.So you can setup this mock with any expectations you want.
For your case you've to substitute DbContext by mock as well
// I hope you have an interface to abstract DbContext?
var dbContextMock = MockRepository.GenerateMock<IDbContext>();
// setup expectations for DbContext mock
dbContextMock.Expect(...)
// bind mock of the DbContext to property of repository.DbContext
ducumentMockRepository.Expect(mock => mock.DbContext)
.Return(dbContextMock)
.Repeat()
.Any();
这篇关于如何嘲笑的DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!