- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个具有 Blob 触发器的 Azure 函数,在我的函数方法参数中,我通过 BlobClient 公开 Blob 本身以及上传的文件的名称。
[FunctionName("MyFunc")]
public async Task RunAsync([BlobTrigger("upload/{name}", Connection = "DataLake")]
BlobClient blob, string name)
{
var propertiesResponse = await blob.GetPropertiesAsync();
var properties = propertiesResponse.Value;
var metadata = properties.Metadata;
//do stuff with metadata
if (metadata.TryGetValue("activityId", out var activityId))
{
}
using (var stream = await blob.OpenReadAsync())
using (var sr = new StreamReader(stream))
{
//do some stuff with blob
}
}
我想对该函数进行单元测试,并尝试模拟 BlobClient,但在使用 Moq 库时遇到问题。我找到了BlobsModelFactory旨在帮助模拟,但我看不到 BlobClient 的任何内容。有人设法模拟 BlobClient 吗?
最佳答案
符合新的Azure SDK guidelines公共(public)方法被标记为virtual
,因此可以模拟它们:
A service client is the main entry point for developers in an Azure SDK library. Because a client type implements most of the “live” logic that communicates with an Azure service, it’s important to be able to create an instance of a client that behaves as expected without making any network calls.
- Each of the Azure SDK clients follows mocking guidelines that allow their behavior to be overridden:
- Each client offers at least one protected constructor to allow inheritance for testing.All public client members are virtual to allow overriding.
如果是 BlobClient
模拟,可以像这样完成*:
var mock = new Mock<BlobClient>();
var responseMock = new Mock<Response>();
mock
.Setup(m => m.GetPropertiesAsync(null, CancellationToken.None).Result)
.Returns(Response.FromValue<BlobProperties>(new BlobProperties(), responseMock.Object))
很多东西,比如 BlobProperties,都可以使用静态类 BlobsModelFactory
来模拟。 ,一些例子:
var blobProps = BlobsModelFactory.BlobProperties(blobType: BlobType.Block);
var result = BlobsModelFactory.BlobDownloadResult(content: null);
其他引用资料:
*代码仅用于演示,引用资料提供了如何使用 BlobsModelFactory 的线索
关于c# - Azure 函数的模拟 BlobClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70632556/
我最近一直在学习 Azure,发现有 2 个用于处理 blob 的类:CloudBlobClient 和 BlobClient。 CloudBlobClient 来自 Microsoft.Azure.
我有一个具有 Blob 触发器的 Azure 函数,在我的函数方法参数中,我通过 BlobClient 公开 Blob 本身以及上传的文件的名称。 [FunctionName("MyFunc")]
我有一个具有 Blob 触发器的 Azure 函数,在我的函数方法参数中,我通过 BlobClient 公开 Blob 本身以及上传的文件的名称。 [FunctionName("MyFunc")]
我有以下代码将文件上传到我的 Azure 存储: 读取文件流如下: FileStream fileStream = File.OpenRead(filePath); 并传递给函数=> publ
我对 C# azure sdk 感到困惑。 我想要实现的目标。 将文件从我的计算机上传到 azure 中的文件夹。 例如 本地 MyFiles -- Folder1 -- file.t
我想将图像上传到Azure,当正确上传后我想删除原始的本地镜像。为此,我需要知道上传何时成功/完成。 docs的BlobClient.uploadWithResponse提及它返回Response 。
我想将图像上传到Azure,当正确上传后我想删除原始的本地镜像。为此,我需要知道上传何时成功/完成。 docs的BlobClient.uploadWithResponse提及它返回Response 。
我见过的大多数(全部?)Azure 存储 Python SDK 示例都演示了创建一个 BlobServiceClient,然后创建一个 BlobClient 来上传/下载 blob( ref1 , r
我想创建一个 Azure SDK BlobClient知道 Blob Uri。我可以这样做: StorageSharedKeyCredential storageCredential = ne
我正在尝试通过内存流将 JSON 上传到 Azure blob。当我调用 UploadAsync 时,我的应用程序挂起。如果我将 UploadAsync 调用移到 StreamWriter 大括号之外
我正在尝试使用 BlobClient 从 Azure 存储中删除多个 Blob blob class . private static void DeleteBlobsFromContainer(Ur
我正在尝试从连接到快速 Wifi 连接的手机上传 200 MB 的视频文件。我正在使用适用于 .NET 的 Azure 存储 SDK v12,但以下代码在上传进度约为 30% 后不断重置自身。当重置发
我注意到在调试跟踪中我当前的方法正在触发 blob 上的 HEAD - 大概是为了检查它是否已经存在。我知道它永远不会,如果它确实存在我想覆盖。我只想直接 PUT 来缩短几毫秒,重要的是减少 Appl
我正在尝试从连接到快速 Wifi 连接的手机上传 200 MB 的视频文件。我正在使用适用于 .NET 的 Azure 存储 SDK v12,但以下代码在上传进度约为 30% 后不断重置自身。当重置发
我注意到在调试跟踪中我当前的方法正在触发 blob 上的 HEAD - 大概是为了检查它是否已经存在。我知道它永远不会,如果它确实存在我想覆盖。我只想直接 PUT 来缩短几毫秒,重要的是减少 Appl
在 Azure Blob 存储中,这两个不同对象上的这两种方法有什么区别?什么时候使用 ContainerInstance.ListBlobsSegmented() 与 CloudBlobClient
我正在创建一个 Azure Function 作为“中间件”,以便最终用户直接在浏览器中打开 Azure Blob 存储中的文件。我使用 AAD 来保护 Azure Function,并创建 SAS
在 Azure 存储 v12 SDK 中,如何使用 Blob URI 和 BlobServiceClient 创建 Blob 客户端? 在 v11 中,我们可以通过以下方式做到这一点: var clo
在 Azure 存储 v12 SDK 中,如何使用 Blob URI 和 BlobServiceClient 创建 Blob 客户端? 在 v11 中,我们可以通过以下方式做到这一点: var clo
我正在从 Azure SDK 11 转换为 12,但在 C# 库中找不到 CloudBlockBlob.IsSnapshot() 的等效项。我怎样才能做等价的? 最佳答案 我想 storage sdk
我是一名优秀的程序员,十分优秀!