Mock IConfigurate in xUnit

If you want mock IConfiguration use following code:

public IConfiguration Configuration { get; }

var mockConfiguration = new Mock<IConfiguration>();
mockConfiguration.SetupGet(x => x[It.Is<string>(s => s == "CMCApiKey")]).Returns("test123");

Configuration = mockConfiguration.Object;

or copy secrets.json configuration file to output directory: projectName\bin\Debug\netcoreapp3.1 and then use following code in ContextFixture:

 var config = new ConfigurationBuilder()
 Configuration = config;

Config json file e.g. secrets.json:

  "CMCApiKey": "test123"

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: