Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@arikkfir
Copy link

@arikkfir arikkfir commented Oct 20, 2022

Summary

This change updates the EventuallyWithT assertion variants (regular, formatted, requirement) to consider a condition as "met" if no assertion errors were raised in a tick.

This allows to write easier conditions which simply contain assertions, without needing to return a bool. The equivalent of a condition returning true in the previous implementation would be a a condition with a single "assert.True(..)" call.

Changes

  • Update all EventuallyWithT variants to consider a condition as "met" if the mocked T (assert.CollectT) has no errors.

Motivation

It's easier to write conditions that only use assertions than conditions that both assert and also need to evaluate the condition result, as the result is often dependent on whether any of the assertions failed. For instance, if an assertion failed, but the condition function returns true, the collection assertions will not fail the test.

Example usage (if applicable)

externalValue := false
go func() {
	time.Sleep(8*time.Second)
	externalValue = true
}()
assert.EventuallyWithTf(t, func(c *assert.CollectT) {
	// add assertions as needed; any assertion failure will fail the current tick
	assert.True(c, externalValue, "expected 'externalValue' to be true")
}, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still: %v", externalState)

Related issues

Related to stretchr#902 and stretchr#1264

This change updates the "EventuallyWithT" assertion variants (regular, formatted,
requirement) to consider a condition as "met" if no assertion errors were raised
in a tick.

This allows to write easier conditions which simply contain assertions, without
needing to return a bool. The equivalent of a condition returning true in the
previous implementation would be a a condition with a single "assert.True(..)" call.
@tobikris
Copy link
Owner

Great idea, thank you very much!

@tobikris tobikris merged commit f9122dc into tobikris:eventually-with-assertions Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants