MyTested.AspNetCore.Mvc icon indicating copy to clipboard operation
MyTested.AspNetCore.Mvc copied to clipboard

Cloudinary account configuration

Open AchoVasilev opened this issue 2 years ago • 4 comments

When I start any test I get this exception:
System.InvalidOperationException : Test application could not be initialized. You may need to create a custom mock for one of your registered services. If you are having difficulties debugging this error, open an issue at https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/issues. Provide your Startup classes and this exception message: 'Cloud name must be specified in Account!'

I have configured a Cloudinary Account in my StartUp and it gets the Account, ApiKey and ApiSecret from the appsettings.json. When debugging the cloudinary account is not configured and it throws the above exception. If I put the values of the Account, ApiKey and ApiSecret in the StartUp class it runs fine. How can I create a mock of this?

I tried this:

public class TestStartUp : Startup { public TestStartUp(IConfiguration configuration) : base(configuration) { }

    public void ConfigureTestServices(IServiceCollection services)
    {
        var cloud = "my cloud";
        var apiKey = "my key";
        var apiSecret = "my secret";
        var cloudinaryAccount = new Account(cloud, apiKey, apiSecret);
        var cloudinary = new Cloudinary(cloudinaryAccount);

        services.ReplaceSingleton<Cloudinary>(cloudinary);
        base.ConfigureServices(services);
    }
} 

But it does not work. I tried also using a CloudinaryMock.Instance but the result was the same.

AchoVasilev avatar Aug 19 '21 07:08 AchoVasilev

You are replacing the Cloudinary service and then calling the original ones, therefore the replacement is no longer valid. You first need to register everything from your base Startup and then replace:

base.ConfigureServices(services);
services.ReplaceSingleton<Cloudinary>(cloudinary);

ivaylokenov avatar Aug 19 '21 10:08 ivaylokenov

I get the same error. When I debug, the debugger goes to my Startup.cs, not the TestStartup.cs and it takes the settings from there and they are null.

AchoVasilev avatar Aug 19 '21 15:08 AchoVasilev

@AchoVasilev Most probably your TestStartup is not located because of namespace/assembly name difference. If you could upload a sample from your solution, I may take a look.

ivaylokenov avatar Nov 25 '21 10:11 ivaylokenov

Here is a link to the entire project - https://github.com/AchoVasilev/Oversteer-Web-Project . I did end up using xUnit in the end due to the project deadline, but would still like to set up this testing library, because I intend to make it a real project-time in due time.

AchoVasilev avatar Nov 25 '21 16:11 AchoVasilev