我正在编写单元测试用例,我必须根据所需的参数返回多个响应。当我尝试下面的代码时,它运行良好。
_mockClient.SetupSequence(c => c.HttpGet(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
.Returns(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(_bucketResponseJson) })
.Returns(new HttpResponseMessage());
Run Code Online (Sandbox Code Playgroud)
但是当我的响应依赖于传递的参数时,我将以下代码与 lambda 表达式一起使用。
_mockClient.SetupSequence(c => c.HttpGet(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
.Returns(((string url, Dictionary<string, string> headers) =>
{
return _objectStoreCache[headers[HeaderValue]] as HttpResponseMessage;
})).Returns(new HttpResponseMessage());
Run Code Online (Sandbox Code Playgroud)
这给了我编译错误:
“无法将 lambda 表达式转换为类型 'System.Net.Http.HttpResponseMessage',因为它不是委托类型”
此锚标记内的图像在WAVE 可访问性检查器中引发“链接图像缺少替代文本”错误:
<a href="www.google.com" title="google link"><img src="google.jpg" alt="" /></a>
Run Code Online (Sandbox Code Playgroud) 我有包含以下键和值的字典。我正在尝试打印字典,但在有空值的地方没有打印任何内容。如何在输出中打印“null”?
Dictionary<string, object> dic1 = new Dictionary<string, object>();
dic1.Add("id", 1);
dic1.Add("name", "john");
dic1.Add("grade", null);
Console.WriteLine(string.Join(Environment.NewLine, dic1.Select(a => $"{a.Key}: {a.Value}")));
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
id: 1
name: john
grade:
Run Code Online (Sandbox Code Playgroud) 使用 NSubstitute,我如何匹配“任何”字典 - 只要它包含一组特定的键值对?
以下内容将匹配任何字典:
mockObject.Received().Method(Arg.Any<Dictionary<string, string>>());
但我希望能够匹配任何字典,只要它具有给定的键值对。例如,我想做类似的事情:
mockObject.Received().Method(Arg.Any<Dictionary<string, string>> { {"MyKey": "MyValue"} });
NSubstitute 中是否存在类似的东西?
如何从 System.windows.Media.Color 对象中提取颜色名称(例如“绿色”)?该.tostring()方法为我提供了十六进制格式 #ff008000。
我发现 ServiceRuntime.RegisterServiceAsync在服务实际注册之前返回并且作为参数传递的工厂 Func 完成之前返回非常不直观。
考虑一下您需要解析正在注册的相同 ServiceName 的场景。你只能从serviceFactory Func中获取的ServiceContext中获取ServiceName。您可能认为一旦获得上下文就可以在 serviceFactory Func 中进行解析。但随后您会进入“死锁”,解析调用将永远陷入困境,因为它等待您正在注册的服务完成。
这就是为什么我期望 RegisterServiceAsync 仅在 serviceFactory Func 完成后才能完成。
c# ×3
dictionary ×2
unit-testing ×2
azure ×1
debugging ×1
html ×1
lambda ×1
moq ×1
nsubstitute ×1
null ×1
nunit-3.0 ×1
testing ×1
w3c ×1
wpf ×1