小编Sup*_*ade的帖子

无法在 NUnit Mock 的 SetupSequence 中使用多次返回

我正在编写单元测试用例,我必须根据所需的参数返回多个响应。当我尝试下面的代码时,它运行良好。

_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',因为它不是委托类型”

c# lambda unit-testing moq nunit-3.0

2
推荐指数
1
解决办法
718
查看次数

使用VS 2019远程调试Azure App Service

如何为在Azure应用服务中运行的.NET应用从VS2019启用调试?该门户仅支持2015和2017。

远程调试选项

debugging azure visual-studio

2
推荐指数
3
解决办法
2754
查看次数

WAVE 错误:链接图像缺少替代文本

此锚标记内的图像在WAVE 可访问性检查器中引发“链接图像缺少替代文本”错误:

<a href="www.google.com" title="google link"><img src="google.jpg" alt="" /></a>
Run Code Online (Sandbox Code Playgroud)

html w3c accessibility

1
推荐指数
1
解决办法
1428
查看次数

如果字典使用 LINQ,如何打印空值

我有包含以下键和值的字典。我正在尝试打印字典,但在有空值的地方没有打印任何内容。如何在输出中打印“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)

c# null dictionary conditional-operator null-coalescing

1
推荐指数
1
解决办法
99
查看次数

NSubstitute:匹配任何带有显式参数的字典?

使用 NSubstitute,我如何匹配“任何”字典 - 只要它包含一组特定的键值对?

以下内容将匹配任何字典:

mockObject.Received().Method(Arg.Any<Dictionary<string, string>>());

但我希望能够匹配任何字典,只要它具有给定的键值对。例如,我想做类似的事情:

mockObject.Received().Method(Arg.Any<Dictionary<string, string>> { {"MyKey": "MyValue"} });

NSubstitute 中是否存在类似的东西?

c# testing dictionary unit-testing nsubstitute

1
推荐指数
1
解决办法
1606
查看次数

从 System.Windows.Media.Color 中提取颜色名称

如何从 System.windows.Media.Color 对象中提取颜色名称(例如“绿色”)?该.tostring()方法为我提供了十六进制格式 #ff008000。

wpf

0
推荐指数
1
解决办法
3366
查看次数

为什么 ServiceRuntime.RegisterServiceAsync 在 serviceFactory 函数完成之前返回?

我发现 ServiceRuntime.RegisterServiceAsync服务实际注册之前返回并且作为参数传递的工厂 Func 完成之前返回非常不直观。

考虑一下您需要解析正在注册的相同 ServiceName 的场景。你只能从serviceFactory Func中获取的ServiceContext中获取ServiceName。您可能认为一旦获得上下文就可以在 serviceFactory Func 中进行解析。但随后您会进入“死锁”,解析调用将永远陷入困境,因为它等待您正在注册的服务完成。

这就是为什么我期望 RegisterServiceAsync 仅在 serviceFactory Func 完成后才能完成。

azure-service-fabric

0
推荐指数
1
解决办法
1880
查看次数