1

When writing Ginkgo unit tests, I ran into an issue asserting data received from a channel that contains a struct with an interface{} field.
Here's my models.go

type WsBaseMsg struct {
    Type    string      `json:"type"` // Discriminator field
    Payload interface{} `json:"payload"`
}

type WsRegistryMsg struct {
    Services LoginsResp
}

type LoginsResp struct {
    Service string    `json:"service"`
    Date    time.Time `json:"date"`
}

And here's ginkgo unit test

expectedMsg := models.WsBaseMsg{
    Type: "NewLogin",
    Payload: []*models.LoginsResp{
    {
        Service: "SomeService",
        Date:    time.Date(2025, time.October, 26, 14, 0, 0, 0, time.UTC),
    },
},
}

wg.Add(1)
go func() {
    defer GinkgoRecover()
    defer wg.Done()
    LoginService.FindLogins(ctx, mockUser, mockConn, resultChan, errChan)}()


Consistently(resultChan,1*time.Second).Should(Receive(BeEquivalentTo(expectedMsg)))

Above FindLogins() returns something like this

<models.WsBaseMsg>: {
          Type: "NewLogin",
          Payload: <[]*models.LoginsResp | len:1, cap:1>[
              {
                  Service: "SomeService",
                  Date: 2025-10-26T14:00:00Z,
              },
          ],
      }

But that assertion failed and give me this message:

RegistersServiceTest FindLoginsServiceTest [It] it should continiously add results channel
/home/cybercrow/Documents/Projects/micro-saas/WhereIDo/server/test/registers_test.go:157

  [FAILED] Failed after 0.000s.
  Expected
      <models.WsBaseMsg>: {
          Type: "NewLogin",
          Payload: <[]*models.LoginsResp | len:1, cap:1>[
              {
                  Service: "SomeService",
                  Date: 2025-10-26T14:00:00Z,
              },
          ],
      }
  to be equivalent to
      <models.WsBaseMsg>: {
          Type: "NewLogin",
          Payload: <[]*models.LoginsResp | len:1, cap:1>[
              {
                  Service: "SomeService",
                  Date: 2025-10-26T14:00:00Z,
              },
          ],
      }

Why assertion failed even expected message comes from that function.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.