мне удалось добавить AppSettings в моем Api проекта из файла appsettings.json
как этотКак использовать appsettings в проекте тестирования ядра nunit dotnet?
Startup.cs в ConfigureServices()
функции
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
Controller.cs
private readonly AppSettings _AppSettings;
public UserProfilesController(IOptions<AppSettings> appSettings)
{
_AppSettings = appSettings.Value;
}
Но я не знаю, как это сделать для моего тестового проекта. У меня нет Startup.ts
в моем тестовом проекте. Итак, как я могу добавить настройки приложения таким же образом в своем тестовом проекте?
EDIT:
Один NUnit тест
[Test]
public void Post_Should_Create_A_Single_UserProfile()
{
// Arrange
var profile = Dummy.GenerateCreateUserProfileDto();
MyMvc
.Controller<UserProfilesController>()
.Calling(c => c.Post(profile))
.ShouldReturn()
.Ok()
.WithResponseModelOfType<UserProfileDto>()
.Passing(target =>
{
target.Should().NotBeNull(because: "a record is expected here");
target.Id.Should().BeGreaterThan(0, because: "a id is expected");
target.ShouldBeEquivalentTo(profile, opt => opt
.Excluding(c => c.Id)
.Excluding(c => c.CreatedOn)
.Excluding(c => c.ModifiedOn),
because: "the record returned is expected to be the same as the record inserted");
// Clean up
_Repo.Delete(target.Id);
});
}
Мой пост функция
[HttpPost]
public async Task<IActionResult> Post([FromBody]CreateUserProfileDto profile)
{
using (var fileManager = new FileManager())
using (var manager = new UserProfilesRepository())
{
var mapped = Mapper.Map<UserProfile>(profile);
// Only save the profile image if one is selected
if (!string.IsNullOrEmpty(profile.Image))
{
try
{
var result = fileManager.SaveProfileImage(
profile.Image,
_AppSettings.Profile.AbsolutePath,
_AppSettings.BaseUrl,
_AppSettings.Profile.RelativePath
);
mapped.FilePath = result.AbsolutePath;
mapped.ProfilePicture = result.RelativePath;
}
catch (Exception ex)
{
return StatusCode(500);
}
}
manager.Save(mapped);
return Ok(Mapper.Map<UserProfileDto>(mapped));
}
}
Показать код для модульного тестирования. Вы можете так же легко высмеять интерфейс IOptions в своих модульных тестах. – Nkosi
@Nkosi Я опубликовал один из тестов nunit и почтовый код контроллера, который использует настройки appsettings. –
Вы используете этот Nunit как часть своего приложения или это другой проект? –