英文:
How do you write tests for checking vars of a specific type?
问题
我有一个返回特定类型客户端的函数,并且我想通过检查返回的变量类型是否为azblob.BlockBlobClient
来测试该函数。
当我使用简单的if
语句来检查类型时,像这样:if var == azblob.BlockBlobClient
,我会得到错误消息azblob.BlockBlobClient (type) is not an expression
。
使用标准的testing
包,有什么正确的方法来测试变量类型?
非常感谢!
英文:
I have a function that returns a specific type of client and I want to test the function by checking if the type of the variable returned is of the type azblob.BlockBlobClient
.
When I use a simple if
statement to check the type like this: if var == azblob.BlockBlobClient
I get the error azblob.BlockBlobClient (type) is not an expression
What's the proper way to test for variable types with the standard testing
package?
Much thanks in advance!
//func
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
}
//test
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")
}
}
</details>
# 答案1
**得分**: 1
你不需要这样做,因为你编写的函数只返回`azblob.BlockBlobClient`类型,编译器会在构建测试之前检查这一点。如果不是这种情况,测试将无法运行。
我对代码进行了以下更改以说明这一点:
### //func
```go
package main
import (
"fmt"
"log"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)
func getClient(blob, container string) interface{} {
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
}
//test
package main
import (
"os"
"testing"
"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)
_, ok := client.(azblob.BlockBlobClient)
if !ok {
t.Errorf("client should be type BlockBlobClient")
}
}
英文:
You don't really need to do this because the function you have wrote only returns the azblob.BlockBlobClient
type, the compiler will check this before even building the tests. The test would fail to run if this was not the case.
I made the following changes to show this:
//func
package main
import (
"fmt"
"log"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)
func getClient(blob, container string) interface{} {
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
}
//test
package main
import (
"os"
"testing"
"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)
_, ok := client.(azblob.BlockBlobClient)
if !ok {
t.Errorf("client should be type BlockBlobClient")
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论