Skip to content

Use of WithinDuration on Equal when having nested time.Time #1388

@hosseio

Description

@hosseio

It is hard to test equality on structs when having instant created time.Time objects in test time. When I test these cases I use to assert equality field by field, and for those time.Time or *time.Time fields I user assert.WithinDuration with some time.Duration delta.

Example

type SubStruct struct {
	Number    int
	TimeField time.Time
}

type TestStruct struct {
	ID        int
	Name      string
	StartTime time.Time
	EndTime   *time.Time
	Sub       SubStruct
}

// to test equality on TestStruct I go with the following

assert.Equal(t, expected.ID, resulted.ID)
// rest of fields
assert.WithinDuration(t, expected.StartTime, resulted.StartTime, time.Second)

What I think would be nice is something like the following (beyond naming)

assert.EqualWithinDuration(t, expected, resulted, time.Second)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementpkg-assertChange related to package testify/asserttype-TimeRelated to type time.Time or the time package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions