我正在测试一个进行Web API调用的服务的方法.HttpClient如果我还在本地运行Web服务(位于解决方案中的另一个项目中),则使用正常工作正常进行单元测试.
但是,当我签入我的更改时,构建服务器将无法访问Web服务,因此测试将失败.
我已经为我的单元测试设计了一种方法,通过创建一个IHttpClient接口并实现我在我的应用程序中使用的版本.对于单元测试,我使用模拟的异步post方法创建一个模拟版本.这是我遇到问题的地方.我想HttpStatusResult为这个特定的测试返回一个OK .对于另一个类似的测试,我将返回一个糟糕的结果.
测试将运行但永远不会完成.它挂在等待.我是异步编程,代表和Moq本身的新手,我一直在搜索SO和谷歌一段时间学习新东西,但我似乎仍然无法解决这个问题.
这是我试图测试的方法:
public async Task<bool> QueueNotificationAsync(IHttpClient client, Email email)
{
// do stuff
try
{
// The test hangs here, never returning
HttpResponseMessage response = await client.PostAsync(uri, content);
// more logic here
}
// more stuff
}
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试方法:
[TestMethod]
public async Task QueueNotificationAsync_Completes_With_ValidEmail()
{
Email email = new Email()
{
FromAddress = "bob@example.com",
ToAddress = "bill@example.com",
CCAddress = "brian@example.com",
BCCAddress = "ben@example.com",
Subject = "Hello",
Body = "Hello …Run Code Online (Sandbox Code Playgroud) 我搜索谷歌和SO没有可用的修复程序.我正在date从SQL Server中的表中检索一个.我需要将它转换为DateTimeDelphi中的对象.问题似乎在于来自SQL的价值.我正在测试它的SQL Server版本是SQL Server 2012(而不是R2).
如果我将查询中检索到的值显示为字符串,我得到:
myQuery.FieldByName('ActiveDate').AsString;
Run Code Online (Sandbox Code Playgroud)
我得到了价值
2014-06-15
Run Code Online (Sandbox Code Playgroud)
myQuery 是TADOQuery类型.
DateTime似乎不喜欢这种格式,因为所有这些都失败了:
myQuery.FieldByName('ActiveDate').AsDateTime;
StrToDateTime(myQuery.FieldByName('ActiveDate').AsString);
StrToDate(myQuery.FieldByName('ActiveDate').AsString);
Run Code Online (Sandbox Code Playgroud)
记录的捕获异常的失败消息是:
''2014-06-15'' is not a valid date and time
Run Code Online (Sandbox Code Playgroud)
有没有一个解决方案,不涉及复杂的字符串操作,不会受到服务器的语言环境的影响?
有没有办法做这个问题正在做的事情(或者或需要验证),但不是将它应用于整个类,我只想在类中的两个特定字段上应用验证。
例如
public class FooModel {
[Required]
public string Name { get; set; }
[Required]
public decimal Cost { get; set; }
public int Bar1 { get; set; }
public float Bar2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我只想将 Bar1 和 Bar2 限制为非此即彼的要求。它们不能都为空。有没有办法做到这一点?
asynchronous ×1
c# ×1
css ×1
css3 ×1
date ×1
datetime ×1
delphi ×1
delphi-xe ×1
html-input ×1
html5 ×1
moq ×1
sql-server ×1
unit-testing ×1