lundi 29 juin 2020

Writing a unit test with golang

So basically, I am trying to mock a unit test on this function right here.

func searchAuthMethod(sep []oktalib.OktaUserAuthnFactor, s string) bool {
    for _, i := range sep {
         if i.FactorType == s {
             return true
         }
     }
     return false
 }

[]oktalib.OktaUserAuthnFactor being an array of struct from a package name oktalib.

type OktaUserAuthnFactor struct {
    Id         string                      `json:"id"`
    FactorType string                      `json:"factorType"`
    Provider   string                      `json:"provider"`
    Embedded   OktaUserAuthnFactorEmbedded `json:"_embedded"`
}

And below is my unit testing code trying to mockwrite the function.

 type okta struct {
      factor string
  }
 
 type authTest struct {
      str string
      Objects []okta
  }
  
  var authTests = []authTest {
      authTest {
       str: "hi",
       Objects: []okta {
              okta {
                 factor:"hi",
              },
          },
      },
  }
  
  func TestSearchAuthMethod (t *testing.T) {
  
         for _, i := range authTests {
             mockData, err := searchAuthMethod(i.Objects.okta , i.str)
             if err != nil {
                 return err
             }  
             if i.Objects.factor != i.str {
                 t.Error("Values do not match")
             }  
        }   
  }

And throws back these errors when typed go test -v in the terminal

# oktassume [oktassume.test]
./writecreds_test.go:48:26: assignment mismatch: 2 variables but searchAuthMethod returns 1 values
./writecreds_test.go:48:55: i.Objects.okta undefined (type []okta has no field or method okta)
./writecreds_test.go:50:16: too many arguments to return
./writecreds_test.go:52:24: i.Objects.factor undefined (type []okta has no field or method factor)

How can I write my test function to get the test to pass, I have been trying to make this work for 2 weeks, will really appreciate it if anyone can help ^.^

Aucun commentaire:

Enregistrer un commentaire