9

I am trying to write the test cases for one of the method written in MVC controller and in that method I am reading AppSettings.json file. as below

public class MemberController : ControllerBase
{
    IMemberRepository _memberRepository;
    public PayPalKeys payPal;
    public ActiveDirectory activeDirectory;
    private static GraphServiceClient _graphServiceClient;
    public MemberController(IMemberRepository member, IOptions<PayPalKeys> payPal, IOptions<ActiveDirectory> activeDirectory)
    {
        _memberRepository = member;
        this.payPal = payPal.Value;
        this.activeDirectory = activeDirectory.Value;
    }   
    
    private IConfigurationRoot LoadAppSettings()
    {
        try {
            var config = new ConfigurationBuilder()
            .SetBasePath(System.IO.Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", false, true)
            .Build();
            -
            -
            -
        }
        catch(Exception ae){}
    }
}

and the below is my Test code

[Fact]
public void Payment()
{
    var memberRepositoryManagerMock = new Mock<IMemberRepository>();     
    var members = new Members() {};
    var controller = new MemberController(memberRepositoryManagerMock.Object, GetPayPalConfigurationMock().Object, GetActiveDirectoryConfigurationMock().Object);
    var result = controller.MakePayment(members);
}

and that method from controller gives the exception as

The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'F:\API.Test\bin\Debug\netcoreapp2.2\appsettings.json'.

Here I am not understanding that how to mock that "appsettings.json" part in the test case code.

3
  • 1
    Is the method your attempting to test calling LoadAppSettings? Because that should really only be called on the start up of the application and not as part of controllers... you are already injecting IOptions<T> so why do you need to rebuild the IConfiguration in the controller? Commented Jun 7, 2019 at 9:38
  • Have you included a line in your csproj (for the test project) to pull the appsettings.json across from your (I'm assuming) main project? Something like the following would do it: <None Include="/path/to/your/appsettings.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="Always" /></ItemGroup> Commented Jun 7, 2019 at 9:45
  • @JamieRees am doing that caouse am modifiyng that appseetings.json file though code, yes i am injecting IOptions<T> but that is of other type. Commented Jun 7, 2019 at 10:26

1 Answer 1

15

Right click on the settings file in Visual Studio and set the "Copy to Output Directory" to "Copy always" or "Copy when never".

enter image description here

To be more precise of the file location, change your configuration-builder to do that:

var config = new ConfigurationBuilder()
    .SetBasePath(AppContext.BaseDirectory)
    .AddJsonFile("appsettings.json", false, true)
    .Build();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.