Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloudinary account configuration #392

Open
AchoVasilev opened this issue Aug 19, 2021 · 4 comments
Open

Cloudinary account configuration #392

AchoVasilev opened this issue Aug 19, 2021 · 4 comments
Assignees
Labels

Comments

@AchoVasilev
Copy link

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.

@ivaylokenov
Copy link
Owner

ivaylokenov commented Aug 19, 2021

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);

@AchoVasilev
Copy link
Author

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.

@ivaylokenov
Copy link
Owner

@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.

@AchoVasilev
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants