I followed the tutorial here but it seems like the start up file does not recognize the appsetting.json file.
So when I run the actual project, the Iconfiguration
have 7 properties.
But when I run the test, it only has one property.
So I was thinking maybe I missed something in the test method to configure the AppSetting.json file..
Here's my test method:
public class StudentServiceRequestsTest
{
private readonly TestServer _server;
private readonly HttpClient _client;
public IndividualServiceRequestsTest()
{
// Arrange
_server = new TestServer(new WebHostBuilder()
.UseStartup<Startup>());
_client = _server.CreateClient();
}
[Fact]
public async Task GetStudentsByDeptShould()
{
try
{
//Act
var response = await _client.GetAsync("/api/department/200/students");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
//Assert
Assert.Equal("hi", responseString);
}
catch (Exception e)
{
throw;
}
}
Here's my startup class, apprently I added the json file which includes all the keys and secrets required in the Web API.
namespace CIS.API.Student
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; set; }
services.AddSingleton(Configuration);
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseMvc();
}
}
}
Anyone knows why it would happen?
WebHostBuilder
'sUseContentRoot
. – Jonathon Chase Apr 23 '18 at 23:38_server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
. I'm assuming your tests are in a different project. ASP.NET is looking for appsettings relative to the location of the running assembly. In the case of a test project, this will likely be in a location that the appsettings.json does not exist, as it wasn't copied on build. – Jonathon Chase Apr 24 '18 at 16:34