Azure Functions的Unittest项目无法加载文件或程序集“Newtonsoft.Json”

4

我按照这里提到的方法为Function App创建了一个测试项目。我所编写的Http触发Azure Function使用了依赖注入(AzureFunctions.AutoFac),代码如下:

[DependencyInjectionConfig(typeof(DependencyConfig))]
public static class CreateDeclarationsFunction
{
    [FunctionName("CreateDeclarationsFunction")]
    public static HttpResponseMessage Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "CreateDeclarations")]HttpRequestMessage req,
        TraceWriter log,
        [Inject]IDeclarationDataService declarationDataService,
        [Inject]IClientDataService clientDataService)
    {
        log.Info("Http triggered function: CreateDeclarationsFunction  processed a request.");

        try
        {
            var clients = clientDataService.GetAll().ToList();

            foreach (var client in clients)
            {
                // Create and save new declaration for each client
                declarationDataService.CreateAndSaveNew(client.Id);
            }
        }
        catch (Exception ex)
        {
            return req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
        }

        return req.CreateResponse(HttpStatusCode.OK);
    }
}

以下为使用 Moq、Shouldly 和 NBuilder 的 unittest 类:

[TestClass]
public class CreateDeclarationsFunctionTest
{
    private Mock<IDeclarationDataService> _declarationDataService;
    private Mock<IClientDataService> _clientDataService;

    [TestInitialize]
    public void Initialize()
    {
        _declarationDataService = new Mock<IDeclarationDataService>();
        _clientDataService = new Mock<IClientDataService>();
    }

    [TestMethod]
    public void CreateDeclarations_ReturnsOk()
    {
        // Arrange
        var clients = Builder<Client>.CreateListOfSize(10).Build();

        _declarationDataService.Setup(x => x.CreateAndSaveNew(It.IsAny<int>()))
            .Returns(Builder<Declaration>.CreateNew().Build);

        // Act > Exception by calling the Run method
        var result = CreateDeclarationsFunction.Run(CreateRequest(""), new TraceWriterStub(TraceLevel.Info),
            _declarationDataService.Object, _clientDataService.Object);

        // Assert
        // TODO
    }

    private static HttpRequestMessage CreateRequest(string json)
    {
        // Just a mocked request
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri("https://localhost"),
            Content = new StringContent(json, Encoding.UTF8, "application/json")
        };

        return request;
    }
}

当我运行这个单元测试时,它会报异常。
Result StackTrace:  
at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter..ctor()
   at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
   at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
   at System.Web.Http.HttpConfiguration.DefaultFormatters(HttpConfiguration config)
   at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes)
   at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Func`2 errorCreator)
   at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Exception exception)
   at FunctionApp.CreateDeclarationsFunction.Run(HttpRequestMessage req, TraceWriter log, IDeclarationDataService declarationDataService, IClientDataService clientDataService)
   at FunctionApp.Tests.CreateDeclarationsFunctionTest.CreateDeclarations_ReturnsOk() in C:\TFS\...\FunctionApp.Tests\CreateDeclarationsFunctionTest.cs:line 63
Result Message: 
Test method FunctionApp.Tests.CreateDeclarationsFunctionTest.CreateDeclarations_ReturnsOk threw exception: 
System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
FunctionApp有2个嵌套引用Newtonsoft.Json (9.0.1),但没有引用版本6.0.0.0Dependencies > Nuget > Microsoft.NET.Sdk.Functions (1.0.6) > Newtonsoft.Json (9.0.1) Dependencies > Nuget > AzureFunctions.Autofac (2.0.0) > Microsoft.Azure.Webjobs (2.1.0-beta4) > Newtonsoft.Json (9.0.1) 测试项目引用了FunctionApp项目。异常只在单元测试时显示,而不是在运行时(且从浏览器调用时)。
有人对此异常有解决方案吗?谢谢。 更新 我发现当返回req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);时,会出现System.IO.FileLoadException
而当返回req.CreateResponse(HttpStatusCode.OK);时,则不会出现上述异常。

2
你的测试项目中是否有一个带有Newtonsoft.Json绑定重定向的app.config文件?在那里添加一个正确的绑定重定向(类似于这个:https://github.com/Azure/azure-webjobs-sdk-script/blob/0b4277b38a89527d21ec9ddd125897013d2542be/test/WebJobs.Script.Tests/app.config#L25-L28)可能会有所帮助。 - Fabio Cavalcante
@FabioCavalcante,确实是这个答案。谢谢! - Melissa
1个回答

3
我认为由于我设置测试项目的方式(最初是使用.NET Standard,然后手动将目标框架转换为.NET Framework 4.6.1,如这里所述),测试项目中没有生成app.config文件。此外,我认为这样的测试项目设置在部分更新解决方案中的nuget包版本时不会自动生成绑定重定向。

正如@FabioCavalcante所提到的,我不得不手动向测试项目添加一个app.config文件,并放置一个Newtonsoft.Json的绑定重定向。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接