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;
ContextFixture.cs

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()
     .AddJsonFile("secrets.json")
     .Build();
 Configuration = config;
AddJsonFile

Config json file e.g. secrets.json:

{
  "CMCApiKey": "test123"
}		
secrets.json

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: