亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何執行 XUnit 測試以檢查和驗證從 AppSetting.json 加載的正確信息

如何執行 XUnit 測試以檢查和驗證從 AppSetting.json 加載的正確信息

C#
斯蒂芬大帝 2022-10-23 15:56:05
我正在使用 SendGrid Azure 規范進行 XUnit 測試,在 .NET CORE 應用程序中運行。我是測試世界的新手,非常了解它背后的哲學。我是負責執行電子郵件發送功能的 EmailService 類。在我的第一個測試中,我想檢查是否從 appsetting.json 文件加載了正確的配置。appsetting.json "SendGridEmailSettings": { "SendGrid_API_Key": "xxx", "SenderConfig": {   "From": "[email protected]",   "Name": "my organization" }}測試班using Xunit;using Xunit.Extensions;namespace Services.Specs{  public class EmailServiceSpec: Specification  {    protected override void Observe()    {        var configuration = Substitute.For<IConfiguration>();        Bag.Configuration = configuration;    }}public class EmailServiceShould: EmailServiceSpec{    public EmailServiceShould()    {        Observe();    }    [Fact]    public void Load_Valid_Configuration_From_AppSetting()    {        //var SendGrid_API_Key = Configuration["SendGridEmailSettings:SendGrid_API_Key"]; this doesn't work    } }}電子郵件服務配置類public interface IEmailConfig{     string SendGrid_API_Key { get; set; }    SenderEmailConfig SenderConfig { get; set; }}電子郵件服務類 public interface IEmailService{           Task SendEmail(EmailMultipleAudience email);    Task SendEmail(EmailSingleAudience email);}public class EmailData{    public string Subject { get; set; }    public string PlainTextContent { get; set; }    public string HtmlContent { get; set; }}public class EmailSingleAudience: EmailData{    public EmailAddress To { get; set; }}public class EmailMultipleAudience : EmailData{    public List<EmailAddress> To { get; set; }}
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

如果您有一個單獨的項目,請復制您appsettings.json的 XUnit 測試項目(否則跳過此步驟)。然后加載它:

var Configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .Build();

然后你就可以使用它了。

var SendGrid_API_Key = Configuration["SendGridEmailSettings:SendGrid_API_Key"];


查看完整回答
反對 回復 2022-10-23
  • 1 回答
  • 0 關注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號