Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.94k stars 441 forks source link

Could not load file or assembly 'System.Net.Http or one of its dependencies while unit testing Azure Function Project #1922

Open sanjaybhagia opened 7 years ago

sanjaybhagia commented 7 years ago

While unit testing Azure Functions Project i get this this exception running my test case:

Could not load file or assembly 'System.Net.Http, Version=4.1.10, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies.

Investigative information

I'm using Visual Studio 2017 15.3.4

Repro steps

Step 1: Create Azure Functions Project for HttpTrigger using VS template (it's targetted at .net framework 4.6.1)

Step 2: Then create a unit test project (4.6.1 framework). Add reference to the azure function projected created above.

Step 3: Run the sample test case You get the exception mentioned above.

Provide the steps required to reproduce the problem:

Expected behavior

Expected behavior is that the test cases runs successfully without complaining about this issue with System.Net.Http dll

Known workarounds

Please check the response from David on this stackoverflow issue that i created. this workaround worked for me.

https://stackoverflow.com/questions/46287628/could-not-load-file-or-assembly-system-net-http-version-4-1-1-0-in-unit-test-fo

In short, i had to create the unit test project using .net core template, edit the csproj file to change the target framework to net461, then everything worked fine.

Related information

Here is the code for unit test that validates the response from azure function

public async Task TestMethod1()
        {
            //Arrange
            var configuration = new HttpConfiguration();
            var req = new HttpRequestMessage()
            {
                Content = new StringContent(string.Empty),
                RequestUri = new Uri("http://localhost?name=test"),
                Method = HttpMethod.Get,
            };

            req.Properties[System.Web.Http.Hosting.HttpPropertyKeys.HttpConfigurationKey] = configuration;
            TraceWriter log = new TraceMonitor();

            //Call azure function
            var result = await TestHttpFunction.Run(req, log).ConfigureAwait(false);
            var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
            Assert.IsTrue(result.IsSuccessStatusCode);
            Assert.AreEqual(content, "\"Hello Test\"");
        }

davidebbo commented 7 years ago

We found a workaround, but still need to understand why the regular unit test project doesn't work. @vijayrkn was going to take a look.

vijayrkn commented 7 years ago

Will provide an update as soon as I find why it doesn't work on regular unit test projects.

vijayrkn commented 7 years ago

Sample function app with tests are available here - https://github.com/Azure/azure-functions-vs-build-sdk/tree/master/sample/FunctionApp

Replace the 1.0.3 version of Functions sdk with 1.0.2 version (which is the most recent version available on NuGet)