簡體   English   中英

如何對時間觸發的 azure function 進行單元測試? 我的測試不起作用

[英]How to unit test azure function triggered by time? My test doesn't work

首先,我的測試代碼:

using Microsoft.AspNetCore.Http;
using Moq;
using NUnit.Framework;
using System.IO;
using System.Threading.Tasks;
using TeamsAllocationManager.Api.Functions;
using TeamsAllocationManager.Contracts.Base;
using TeamsAllocationManager.Contracts.EmployeeWorkingTypeHistory;
using Microsoft.Extensions.Logging;
using System.Linq.Expressions;
using System;

namespace TeamsAllocationManager.Tests.Functions
{
    [TestFixture]
    public class HistoryClearingFunctionTests
    {
        private readonly ILogger _mockedLogger;
        private readonly Mock<IDispatcher> _dispatcherMock;

        public HistoryClearingFunctionTests()
        {
            _mockedLogger = new Mock<ILogger>().Object;
            _dispatcherMock = new Mock<IDispatcher>();
        }

        [Test]
        public async Task ShouldCallClearOldEmployeeWorkingTypeHistoryRecordsCommand() =>
            await VerifyFunctionExecutionAsync(c => c.DispatchAsync<ClearOldEmployeeWorkingTypeHistoryRecordsCommand, bool>(It.IsAny<ClearOldEmployeeWorkingTypeHistoryRecordsCommand>(), default), "GET");
        private async Task VerifyFunctionExecutionAsync(Expression<Action<IDispatcher>> expression, string verb, string path = "", QueryCollection? query = null, MemoryStream? body = null)
        {
            // given
            var function = new HistoryClearingFunction(_dispatcherMock.Object);
            var reqMock = new Mock<HttpRequest>();
            reqMock.Setup(r => r.Method).Returns(verb);
            reqMock.Setup(r => r.Query).Returns(query ?? new QueryCollection());
            reqMock.Setup(r => r.Body).Returns(body ?? new MemoryStream());

            // when
            await function.RunAsync(reqMock.Object, path, _mockedLogger);

            // then
            _dispatcherMock.Verify(expression, Times.Once);
        }
    }
}

目前我在第 29 行有一個錯誤,我在那里等待 VerifyFunctionExecutionAsync。 錯誤出現在 DispatchAsync<Clear***Command> 上,聽起來像這樣:

“TeamsAllocationManager.Contracts.EmployeeWorkingTypeHistory.ClearOldEmployeeWorkingTypeHistoryRecordsCommand”不能用作泛型類型或方法“ICommandDispatcher.DispatchAsync(TCommand, CancellationToken)”中的類型參數“TCommand”。 沒有從“TeamsAllocationManager.Contracts.EmployeeWorkingTypeHistory.ClearOldEmployeeWorkingTypeHistoryRecordsCommand”到“TeamsAllocationManager.Contracts.Base.Commands.ICommand”的隱式引用轉換。 TeamsAllocationManager.Tests

我是 azure 功能的新手,不知道如何修復它。 rest 的代碼(測試功能)工作正常,所以只有測試有問題。

#Update 1 修復了上面代碼中的錯誤,並在下面添加了 function 代碼:

using Microsoft.Azure.WebJobs;
using System.Threading.Tasks;
using TeamsAllocationManager.Contracts.Base;
using TeamsAllocationManager.Contracts.EmployeeDeskHistory;
using TeamsAllocationManager.Contracts.EmployeeWorkingTypeHistory;

namespace TeamsAllocationManager.Api.Functions
{
    public class HistoryClearingFunction : FunctionBase
    {
        private readonly IDispatcher _dispatcher;
        public HistoryClearingFunction(IDispatcher dispatcher)
        {
            _dispatcher = dispatcher;
        }
        [FunctionName("EmployeeWorkingTypeHistoryClearingFunction")]
        public async Task EmployeeWorkingTypeHistoryClearingFunction([TimerTrigger("0 0 0 1 * *")]TimerInfo myTimer) 
        {
            await _dispatcher.DispatchAsync<ClearOldEmployeeWorkingTypeHistoryRecordsCommand, bool>(new ClearOldEmployeeWorkingTypeHistoryRecordsCommand());
        }
        [FunctionName("EmployeeDeskHistoryClearingFunction")]
        public async Task EmployeeDeskHistoryClearingFunction([TimerTrigger("0 0 0 1 * *")] TimerInfo myTimer)
        {
            await _dispatcher.DispatchAsync<ClearOldEmployeeDeskHistoryRecordsCommand, bool>(new ClearOldEmployeeDeskHistoryRecordsCommand());
        }
    }
}

當前錯誤:

消息:Moq.MockException:預期在模擬上調用一次,但為 0 次:c => c.DispatchAsync<ClearOldEmployeeWorkingTypeHistoryRecordsCommand, CancellationToken>(It.IsAny()

執行的調用:

MockIDispatcher:1 (c):

未執行任何調用。

我建議通過架構更改來解決這個問題。

將所有與 function 技術本身不直接相關的功能代碼移動到幫助程序 class 中。 然后針對這個助手 class 編寫 UnitTest。

這將使測試執行生產性工作的代碼變得更加容易。

暫無
暫無

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

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