1

I have one problem with integration testing in new .net core framework.

I've put some custom data in appsettings.json config and wanted to use it in some class.

"Data": {
  "FolderName": "Data",
  "Files": {
    "Json": "work.json",
    "Csv": "work.csv"
  }
}

But when I do getting of the data inside class using IConfiguration interface - folderName variable is empty.

private void InitializeDataFileFullPath(IConfiguration configuration)
    {
        var root = Directory.GetCurrentDirectory();

        var folderName = configuration["Data:FolderName"];
        if (folderName == null)
        {
            throw new NullReferenceException("Folder name with data files isn't specified in appsettings.json");
        }

Also root variable shows not right path - ".IntegrationTests\bin\Debug\netcoreapp2.0", but I expected to see something like \.WebApi\

I followed this manual. I also wanted to do something like there, but there's no project.json file anymore.

P.S.

  1. If I run project just under Visual Studio - everything is fine.
  2. My fixture class is absolutely the same as in manual from Microsoft. I thought all that manipulations provide me to have project which is being tested in right directory at least.

Thanks for any advice.

(added) The main thing of my post is that getting from appsettings shows different values for integration testing and for common running of the program. I want it to be the same.

cortisol
  • 335
  • 2
  • 18

3 Answers3

0

Are you using the Development version of appsettings ?

By default an ASP.NET Core project comes with two appsettings.json.

Example in Visual Studio 2017

Exemple in Visual Studio 2017

cortisol
  • 335
  • 2
  • 18
0

My best guess is that you have two executables: one is your web app, and the other is your integration test project. Each executable needs its own appsettings.json: your integration test executable can't magically find the appsettings.json from your web app project.

What's strange is that if you follow the guide you mention, the GetProjectPath method should find the appropriate path to create your configuration object.

Métoule
  • 13,062
  • 2
  • 56
  • 84
  • "GetProjectPath method should find the appropriate path to create your configuration object." - it found, but I don't understand why appsettings isn't been copied in test directory or just found. Because all what described in that article is for doing integration testing more closely to real conditions...Maybe it's a bug. – cortisol Feb 04 '18 at 16:45
  • 1
    No file is automatically copied to the output directory, but it's easy to do so: right click on file, properties, Copy to output directory : always. – Métoule Feb 04 '18 at 16:48
  • Thanks for advice, I'll try it. – cortisol Feb 04 '18 at 17:00
-1
  • Use Configuration.GetSection("Data:FolderName").Value.ToString();
  • Directory.GetCurrentDirectory() will return the folder from where the .exe is running.
TahirRauf
  • 78
  • 1
  • 9
  • Did you read P.S.? The main thing of my post is that getting from appsettings shows different values for integration testing and for common running of the program. I need it to be the same. – cortisol Feb 03 '18 at 19:35