我有一个返回特定类型客户端的函数,我想通过检查返回的变量类型是否为 type 来测试该函数azblob.BlockBlobClient。
当我使用一个简单的if语句来检查这样的类型时:if var == azblob.BlockBlobClient我得到了错误azblob.BlockBlobClient (type) is not an expression
testing使用标准包测试变量类型的正确方法是什么?
非常感谢!
//函数
func getClient(blob, container string) azblob.BlockBlobClient {
storageAccount := os.Getenv("AZURE_STORAGE_ACCOUNT_NAME")
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatal("Invalid credentials with error:" + err.Error())
}
blobUrl := fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s", storageAccount, container, blob)
fmt.Println(blobUrl)
client, err := azblob.NewBlockBlobClient(blobUrl, cred, nil)
if err != nil {
log.Fatal("Unable to create blob client")
}
return client
}
//测试
package main
import (
"testing"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)
func TestgetClient(t *testing.T){
blob := "text.txt"
container := "testcontainer"
os.Setenv("AZURE_STORAGE_ACCOUNT_NAME", "mystorageaccount")
client := getClient(blob, container)
if client != azblob.BlockBlobClient {
t.ErrorF("Client should be type BlockBlobClient")
}
}
莫回无
相关分类