如何编写测试以检查特定类型的变量?

huangapple go评论84阅读模式
英文:

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 (
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;os&quot;

	&quot;github.com/Azure/azure-sdk-for-go/sdk/azidentity&quot;
	&quot;github.com/Azure/azure-sdk-for-go/sdk/storage/azblob&quot;
)

func getClient(blob, container string) interface{} {
	storageAccount := os.Getenv(&quot;AZURE_STORAGE_ACCOUNT_NAME&quot;)

	cred, err := azidentity.NewDefaultAzureCredential(nil)
	if err != nil {
		log.Fatal(&quot;Invalid credentials with error:&quot; + err.Error())
	}

	blobUrl := fmt.Sprintf(&quot;https://%s.blob.core.windows.net/%s/%s&quot;, storageAccount, container, blob)
	fmt.Println(blobUrl)
	client, err := azblob.NewBlockBlobClient(blobUrl, cred, nil)
	if err != nil {
		log.Fatal(&quot;Unable to create blob client&quot;)
	}
	return client
}

//test

package main

import (
	&quot;os&quot;
	&quot;testing&quot;

	&quot;github.com/Azure/azure-sdk-for-go/sdk/storage/azblob&quot;
)

func TestgetClient(t *testing.T) {
	blob := &quot;text.txt&quot;
	container := &quot;testcontainer&quot;
	os.Setenv(&quot;AZURE_STORAGE_ACCOUNT_NAME&quot;, &quot;mystorageaccount&quot;)
	client := getClient(blob, container)

	_, ok := client.(azblob.BlockBlobClient)
	if !ok {
		t.Errorf(&quot;client should be type BlockBlobClient&quot;)
	}
}

huangapple
  • 本文由 发表于 2022年3月28日 22:59:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/71649523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定