簡體   English   中英

Moq,測試方法返回錯誤 - System.ArgumentNullException:值不能為空。 (參數“來源”)

[英]Moq, test method return error - System.ArgumentNullException: Value cannot be null. (Parameter 'source')

我有以下服務:

DocumentTypeService

public partial class DocumentTypeService : IDocumentTypeService
{
    private readonly IRepository<DocumentType> _documentTypeRepository;
    private readonly IMediator _mediator;

    public DocumentTypeService(IRepository<DocumentType> documentTypeRepository, IMediator mediator)
    {
        _documentTypeRepository = documentTypeRepository;
        _mediator = mediator;
    }

    public virtual async Task<IList<DocumentType>> GetAll()
    {
        var query = from t in _documentTypeRepository.Table
            orderby t.DisplayOrder
            select t;
        return await query.ToListAsync();
    }
}

GetAll()測試GetAll()方法

[TestClass()]
public class DocumentTypeServiceTests
{
    private Mock<IRepository<DocumentType>> _documentTypeRepositoryMock;
    private DocumentTypeService _documentTypeService;
    private Mock<IMediator> _mediatorMock;

    [TestInitialize()]
    public void Init()
    {
        _mediatorMock = new Mock<IMediator>();
        _documentTypeRepositoryMock = new Mock<IRepository<DocumentType>>();
        _documentTypeService = new DocumentTypeService(_documentTypeRepositoryMock.Object, _mediatorMock.Object);
    }


    [TestMethod()]
    public async Task GetAllDocumentTypes()
    {
        await _documentTypeService.GetAll();
        _documentTypeRepositoryMock.Verify(c => c.Table, Times.Once);
    }
}

GetAllDocumentTypes()方法返回的錯誤

測試方法 Grand.Services.Tests.Documents.DocumentTypeServiceTests.GetAllDocumentTypes 拋出異常:
System.ArgumentNullException:值不能為空。 (參數“來源”)
堆棧跟蹤:
Queryable.OrderBy[TSource,TKey](IQueryable'1 source, Expression'1 keySelector)
MongoQueryable.OrderBy[TSource,TKey](IMongoQueryable'1 source, Expression'1 keySelector)

更新:

/// <summary>
/// MongoDB repository
/// </summary>
public partial class Repository<T> : IRepository<T> where T : BaseEntity
{
    /// <summary>
    /// Gets a table
    /// </summary>
    public virtual IMongoQueryable<T> Table
    {
        get { return _collection.AsQueryable(); }
    }
}

您應該模擬以下方法調用:

_documentTypeRepository.Table

具體來說,你需要這樣的東西:

_documentTypeRepositoryMock = new Mock<IRepository<DocumentType>>();
var mongoQueryableMock = new Mock<IMongoQueryable<DocumentType>>();
_documentTypeRepositoryMock.Setup(x=>x.Table).Returns(mongoQueryableMock);

這樣做,當_documentTypeRepository.Table被調用時將返回一個空的DocumentType列表,不需要調用 OrderBy。 默認行為是,因為您不模擬此調用, _documentTypeRepository.Table返回 null。 因此,稍后會出現空指針異常。

為了理解后一個參數,您應該了解查詢語法如何在編譯時工作。 查詢語法只是語法糖。 編譯代碼時,首先將其替換為等效的(“LINQ”)方法調用,並生成相應的 IL 代碼。 所以你的查詢:

from t in _documentTypeRepository.Table
orderby t.DisplayOrder
select t;

將首先在如下查詢中轉換:

_documentTypeRepository.Table
                       .OrderBy(x=>x)
                       .Select(x=>x)

稍后將根據后一個查詢生成相應的 IL 代碼。

因此,如果_documentTypeRepository.Table返回 null,則調用OrderBy將引發空指針異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM