我在服务层有一个方法来连接到服务,并且我正在使用IHttpClientFactory
. 我的方法运行良好。现在我正在尝试为此编写单元测试用例。
public async Task<MyObject> MyMethodAsync(string arg1, string arg2)
{
var client = _httpClientFactory.CreateClient("XYZ");
var Authkey = "abc";
var AuthToken = "def";
var headers = new Dictionary<string, string>
{
{ Authkey,AuthToken }
};
client.AddTokenToHeader(headers); //This method set the DefaultRequestheader from the dictionary object
var reqData = new
{
prop1 = "X",
prop2 = "Y"
};//req object
var content = new StringContent(JsonConvert.SerializeObject(reqData), Encoding.UTF8, "application/json");
//This is httpClient Post call for posting data
HttpResponseMessage response = await client.PostAsync("postData", content);
if …
Run Code Online (Sandbox Code Playgroud) c# unit-testing dotnet-httpclient asp.net-core httpclientfactory
如何简化空值检查
public class MyEmployee
{
public string FirstName;
public string LastName;
public string Age;
public string Phone;
public string Gender;
}
Run Code Online (Sandbox Code Playgroud)
我已经实现了以下空条件检查
public async Task<bool> ValidateClient(MyEmployee Client)
{
**if(Client.FirstName == null ||Client.LastName==null ||Client.Age== null ||Client.Gender ==null||Client.Phone==null)**
{
throw new Argumentexception("Employee details to be provided")
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 null 检查条件验证 Employee 类的所有属性,这可以在 C# 中简化吗?
有没有更好的方法来使用 Null Coalescing 运算符检查字符串是否为空或 null。当我对字符串使用空值而不是 null 时,null Coalescing 运算符无法获得所需的结果。
string x = null;
string y = x ?? "Default Value";
Console.WriteLine(y);
Run Code Online (Sandbox Code Playgroud)
在这里,如果我x = null
用x=""
这个替换,则不起作用。
如果我使用String.IsNullOrEmpty
方法
if(String.IsNullOrEmpty(x)
{
y= "default value"
}
{
y =x
}
Run Code Online (Sandbox Code Playgroud)
我的代码块有多行,我想让它变得简单。您能建议一种更好的方法来保持代码干净简单吗?因为它被用在很多地方。
我正在尝试通过参考链接创建 ON-OFF 开关
在这里,我无法在 index.html 文件中使用 FontAwesome 引用,我所做的是在我的项目中复制了 font-awesome-css 文件。但是开关ON-OFF按钮没有显示。
在这里,我从 index.html 中删除了引用,并将 font-awesome.min.css 的内容复制到我的 style.css 文件中我的要求是根据模型值(布尔值)创建 ON-OFF 开关。
它应该有编辑(开/关)功能。
任何人都可以建议为什么 css 文件不起作用,或者我还有其他方法可以做到这一点。
css angularjs toggleswitch angular-ui-bootstrap fontawesome-4.4.0
c# ×3
asp.net-core ×2
.net ×1
.net-core ×1
angularjs ×1
css ×1
toggleswitch ×1
unit-testing ×1