我有async没有任何返回类型的方法,它只有Task类型.
我怎样才能编写有效的单元测试方法Task?
异步方法:
public async Task UploadFileAsync(FileDetails data)
{
await _fileUpload.UploadImageAsync(data);
}
public async Task UploadImageAsync(FileDetails data)
{
// does a async operation to upload a stream to azure blob storage
await azureBlob.UploadFromStreamAsync(streamData).ConfigureAwait(false)
}
Run Code Online (Sandbox Code Playgroud)
测试方法:
[TestMethod]
public async Task UploadFileAsync()
{
//-->Arrange
_fileUpload.Setup(e => e.UploadImageAsync().Returns(new Task<Task>(() => null));
//-->Act
Task result = _uploadManager.UploadFileAsync(GetFakeUploadData());
// Task doesn't return any result ?
//-->Assert
}
Run Code Online (Sandbox Code Playgroud)
更新:
最后我调用此方法将文件上传到azure blob存储.
string search = "Apple : 100";
string[] result = search .Split(':');
Run Code Online (Sandbox Code Playgroud)
适用于以下输出:
result[0] ==> Apple
result[1] ==> 100
Run Code Online (Sandbox Code Playgroud)
但为此:
string search = "Apple";
string[] result = search .Split(':');
Run Code Online (Sandbox Code Playgroud)
输出:
result[0] ==> Apple
Run Code Online (Sandbox Code Playgroud)
为什么输出是Apple?如果搜索中缺少分隔符,我只想要空数组string.
任何帮助,将不胜感激.
我试图将html <li>标签作为前缀和后缀添加到字符串列表的每个元素.
这是字符串列表:
IList<string> lstUniversities = new List<string>();
lstUniversities.Add("<sup>1</sup>Harward University, USA");
lstUniversities.Add("<sup>2</sup>Yale University, USA");
lstUniversities.Select(u => "<li> " + u + " </li>").ToList();
Run Code Online (Sandbox Code Playgroud)
预期产出:
<li><sup>1</sup>Harward University, USA</li>
<li><sup>2</sup>Yale University , USA</li>
Run Code Online (Sandbox Code Playgroud)
我可以知道为什么html <li>不附加标签吗?
我有国家列表,里面有地方列表
...
public IList<ICountriesDTO> Countries { get; set; }
public class CountriesDTO: ICountriesDTO
{
public IEnumerable<IPlacesDTO> Places { get; set;
}
Run Code Online (Sandbox Code Playgroud)
我想获取地方列表如果没有 null
allPlacesDTO.World.Countries.SelectMany(x => x.Places == null ? null : x.Places).ToList();
Run Code Online (Sandbox Code Playgroud)
但它给出的null exception地方是null为国家object.
我如何null检查Places并只使用return语句而不是选择null object,如下所示?
if (allPlacesDTO.World.Countries.Places == null)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
更新:
我的要求是,如果在任何一个国家没有地方只使用return声明退出当前功能而不继续进行.这是通过公认的答案和Count功能实现的.
var lstAllPlaces = allPlacesDTO.World.Countries.Where(x => x.Places != null).SelectMany(x => x.Places).ToList();
if (lstAllPlaces.Count() == 0)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
谢谢大家的答案.欣赏.
我试图string根据boolean属性值进行逗号分隔model.
码
string options = string.Empty;
if (model.Fruits.Value)
{
options += "FR,";
}
if (model.Drinks.Value)
{
options += "DR,";
}
if (model.Lunch.Value)
{
options += "LU,";
}
if (model.Dinner.Value)
{
options += "DI,";
}
Run Code Online (Sandbox Code Playgroud)
我们可以使上面的代码基于简写,使用ternary conditional operator (?:)或者我应该保留上面的代码,因为它更具可读性?
任何建议都会很棒.
我想创建一个匿名列表,它可以保存任何data type与intellisense支持,但没有创造class。
所以我找到了以下使用匿名类型的解决方案。
var list = new[]
{
new { Number = 10, Name = "Smith" },
new { Number = 10, Name = "John" }
}.ToList();
foreach (var item in list)
{
Console.WriteLine(item.Name);
}
Run Code Online (Sandbox Code Playgroud)
但是如果我想要一个返回匿名类型以上的方法怎么办。
public List<object> GetData()
{
var list = new[]
{
new { Number = 10, Name = "Smith" },
new { Number = 10, Name = "John" }
}.ToList();
return list;
Run Code Online (Sandbox Code Playgroud)
}
编译时间错误:
无法将类型“System.Collections.Generic.List<>”隐式转换为“System.Collections.Generic.List”
是否可以将匿名类型列表转换为具有智能感知支持的对象列表?
更新:
我们不想创建类型的原因,因为我们只想使用匿名类型进行一些数据操作并填充其他一些对象。
c# ×6
.net ×3
append ×1
async-await ×1
if-statement ×1
linq ×1
linq-to-sql ×1
list ×1
split ×1
string ×1
unit-testing ×1