Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce duplication of testing.T argument #38

Open
eberkund opened this issue Dec 17, 2021 · 0 comments
Open

Reduce duplication of testing.T argument #38

eberkund opened this issue Dec 17, 2021 · 0 comments

Comments

@eberkund
Copy link

eberkund commented Dec 17, 2021

It would be nice if the next version used a syntax similar to stretchr/testify. Since t is passed in to New() we shouldn't need to pass it in again for each assertion.

See this example from stretchr/testify:

package yours

import (
  "testing"
  "github.com/stretchr/testify/assert"
)

func TestSomething(t *testing.T) {
  assert := assert.New(t)

  // assert equality
  assert.Equal(123, 123, "they should be equal")

  // assert inequality
  assert.NotEqual(123, 456, "they should not be equal")

  // assert for nil (good for errors)
  assert.Nil(object)

  // assert for not nil (good when you expect something)
  if assert.NotNil(object) {

    // now we know that object isn't nil, we are safe to make
    // further assertions without causing any errors
    assert.Equal("Something", object.Value)
  }
}

For goldie it would look like this:

func TestExample(t *testing.T) {
    recorder := httptest.NewRecorder()

    req, err := http.NewRequest("GET", "/example", nil)
    assert.Nil(t, err)

    handler := http.HandlerFunc(ExampleHandler)
    handler.ServeHTTP()

    g := goldie.New(t)
    g.Assert("example", recorder.Body.Bytes()) // notice lack of `t` arg here.
}

If there is interest, I would be happy to make a PR.

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

No branches or pull requests

1 participant