1

I am trying to write a unit test for the following:

[TestMethod]
public void GetInviteEndPoint_ShouldAccessAppSettings()
{
    //Data pulled from the appsettings.test.json
    var config = InitConfiguration();
    var inviteEndPointConfig = config["InviteEndPoint"]; // <-- Pain Point

    //Arrange Test && Mock if needed
    string mockInviteEndPoint = "https://graph.microsoft.com/v1.0/invitations";


    //Actual Code from Application (ACT)
    SendInvite sendInvite = new SendInvite();
    string inviteEndPoint = sendInvite.GetInviteEndPoint(config);

    //Assert 
    // Assert always tests (Expected[Arranged], Actual[From Code Base])
    Assert.AreEqual(mockInviteEndPoint, inviteEndPoint);
}

My both my appsettings.json and appsettings.test.json look identical. I am having a hard time getting the value from the .json file. I was wondering if anybody could provide any insight on this code that I am stuck on.

{
    "SendeInvite": {
        "InviteEndPoint": "https://graph.microsoft.com/v1.0/invitations"
        ...Code Omitted... 
    } 
}

Am I calling the config["InvitedEndPoint"] incorrectly?

Please note I have the following code at the top of the Test Class

public static IConfiguration InitConfiguration()
{
    var config = new ConfigurationBuilder()
        .AddJsonFile("appsettings.test.json")
        .Build();
    return config;
}
ekad
  • 14,436
  • 26
  • 44
  • 46
Moojjoo
  • 729
  • 1
  • 12
  • 33
  • 1
    What is the problem? Do you get an exception, is `inviteEndPointConfig` null? Are you *sure* you're loading the correct file? `"appsettings.test.json"` is a *relative* path which means the test runner will look for it in *its* working directory. Even if that is `bin/Debug` you need to ensure `appsettings.test.json` is copied to `bin/debug` – Panagiotis Kanavos Jan 30 '19 at 14:21
  • You tagged your question nunit, but it's apparently using Microsoft's test framework, based on your use of the TestMethodAttribute. Please re-tag. – Charlie Jan 30 '19 at 18:08

1 Answers1

1

Try:

var inviteEndPointConfig = config["SendeInvite:InviteEndPoint"];

Probably because you nested the Attribute in SendeInvite you don't get the value.

Mike
  • 174
  • 1
  • 11
  • I made some changes based on this post - https://stackoverflow.com/questions/46940710/getting-value-from-appsettings-json-in-net-core and will return with the results. – Moojjoo Jan 30 '19 at 14:53
  • 1
    With the combination of your post and the link I posted I was able to make the test pass. Starting to get my brain rapped around this Depdency Injection DI. Any body have recommendation on some DI and IC readings that are really really good and explaining the subject? – Moojjoo Jan 30 '19 at 14:59