Home > Software engineering >  How do you write tests for checking vars of a specific type?
How do you write tests for checking vars of a specific type?

Time:03-29

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")
  }
}

CodePudding user response:

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")
    }
}
  • Related