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

时间:2017-11-14 14:37:08

标签: c# .net unit-testing azure azure-functions

我为here提到的功能应用程序创建了一个测试项目。 Http触发了我编写的Azure函数,使用依赖注入(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,应该和NBuilder):

[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有两个Newtonsoft.Json (9.0.1)的嵌套引用,但没有一个版本6.0.0.0

Dependencies&gt; Nuget&gt; Microsoft.NET``.Sdk.Functions (1.0.6)&gt; Newtonsoft.Json (9.0.1)

Dependencies&gt; Nuget&gt; AzureFunctions.Autofac (2.0.0)&gt; Microsoft.Azure.Webjobs (2.1.0-beta4)&gt; Newtonsoft.Json (9.0.1)

测试项目引用了FunctionApp项目。该异常仅在单元测试时显示,而不在其运行时显示(并从浏览器调用)。 有没有人有上层例外的解决方案?谢谢。

更新

我发现返回System.IO.FileLoadException

时会发生req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);

当返回req.CreateResponse(HttpStatusCode.OK);时,它不会提供上述异常。

1 个答案:

答案 0 :(得分:3)

我认为,由于我设置测试项目的方式(最初.NET standard然后将目标框架手动转换为.NET Framework 4.6.1,如上所述here),没有app.config文件在测试项目中生成。此外,我认为在解决方案中部分更新nuget软件包的版本时,这样的测试项目设置不会自动为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>