asp.net核心webapi MsTest

时间:2020-04-30 13:22:30

标签: c# asp.net-core asp.net-core-webapi mstest

我尝试使用MsTest来测试asp.net核心webapi

  </script>
 document.addEventListener('DOMContentLoaded', function(){     
 google.script.run.withSuccessHandler(populateCodes).getCodes(); });

function populateCodes(words)
{
  var autocomplete = document.getElementById("InputCode");
  var instances = M.Autocomplete.init(autocomplete,{data : words});
}

function FilterTables() {
  var input, filter, table, tr, td, i;
  input = document.getElementById("InputCode");
  filter = input.value;
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[1];
    if (td) {
      if (td.innerHTML.indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }       
  }
}
function getCodes()
{
  var id = "1L33jDRFmDxEf3Q-RYR8uyQe6IVkSDkSU09YKusZ_8Gw";
  var ss = SpreadsheetApp.openById(id);
  var wsData = ss.getSheetByName("Data");
  var RetCodes = wsData.getRange(2, 2,wsData.getRange('B2').getDataRegion().getLastRow(),1).getValues();
  var options = {};
  var CodesR = RetCodes.map(function(o){return o[0];})
  RetCodes.forEach(function(v)
                   {
    options[v[0]] = null;

                    });
  return options;

  }
  </script>


<html>
  <head>
    <base target="_top">
         <meta charset="utf-8">
           <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
            <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
              <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
                <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
          <script src="https://www.w3schools.com/lib/w3.js"></script>
        <script src="https://www.w3schools.com/lib/w3.js"></script>
            <?!= include("page-css"); ?> 
  </head>
<body>
 <?!= include("page-js"); ?>
    <div class="row"> 
       <div class = "container">
          <div class="input-field col s3">
             <i class="material-icons prefix">textsms</i>
                <input type="text" id="InputCode"  style="width: auto alignment: center" >
                <label id = "InputLabel" for="InputCode"></label>  
             <input id='RClick' type='button' name='button' value='Details' onclick="FilterTables()">
          </div>
       </div>
    </div> 

    <div class = "container">
       <div class="input-field col s8">
          <table id="myTable">
            <?!= tablecells ?> 
          </table>
       </div>
    </div>
</body>
</html>

我使用此代码测试webapi。我的目的是查看console.WriteLine。 但是,asp.net核心具有默认输出的日志记录。 所以我看到了。

  [TestClass]
public class IndexControllerTest
{
    private readonly WebHostTestHelp _webhost;

    public IndexControllerTest( )
    {
        _webhost =new  WebHostTestHelp();
    }
    [TestMethod]
    public async Task GetindexstringhttpTest()
    {
        var res = await _webhost._http.GetAsync("api/Index/indexstring");
        var result = await res.Content.ReadAsStringAsync();
        Console.WriteLine(result);
        Assert.AreEqual("OK", res.StatusCode.ToString());
        Assert.AreEqual("indexstring", result);
        res.isCodeOk();
    }
    [TestMethod]
    public async Task GetindexresultdatahttpTest()
    {
        var res = await _webhost._http.GetAsync("api/Index/indexresultdata");
        var result = await res.Content.ReadAsStringAsync();
        Console.WriteLine(result);
        Assert.AreEqual("OK", res.StatusCode.ToString());
        res.isCodeOk();
    }
}

我不需要查看警告,信息或其他信息。 请告诉我该怎么做。谢谢。

我更改了代码即可使用。

 warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
      Failed to determine the https port for redirect.
info: DataBase.FilterHelp.LogFilterAttribute[0]
       0 | zekj.Controllers.IndexController.indexresultdata (zekj) | {"state":"查询","affectRow":0,"countRow":1,"message":{"data":"2222indexresultdata"}}
{"state":"查询","affectRow":0,"countRow":1,"message":{"data":"2222indexresultdata"}}

1 个答案:

答案 0 :(得分:0)

我认为您在应用设置json中有一个设置,这与默认的日志记录级别有关。您可以共享记录设置吗,然后我可以更新答案,也可以尝试将级别更改为错误。