Skip to content

Instantly share code, notes, and snippets.

@agusrichard
Created June 6, 2021 21:56
Show Gist options
  • Select an option

  • Save agusrichard/bcf352aec5b5271a59c1a037c4ca079c to your computer and use it in GitHub Desktop.

Select an option

Save agusrichard/bcf352aec5b5271a59c1a037c4ca079c to your computer and use it in GitHub Desktop.
type tweetUsecaseSuite struct {
// we need this to use the suite functionalities from testify
suite.Suite
// the generated mocked version of our repository
repository *mocks.TweetRepository
// the functionalities we want to test
usecase TweetUsecase
}
func (suite *tweetUsecaseSuite) SetupTest() {
// instantiate the mocked version of repository
repository := new(mocks.TweetRepository)
// inject the repository to usecase, since usecase needs repository to work
usecase := InitializeTweetUsecase(repository)
// assign them as the suite properties
suite.repository = repository
suite.usecase = usecase
}
func (suite *tweetUsecaseSuite) TestCreateTweet_Positive() {
// create an example of tweet that will be used in usecase's CreateTweet method
tweet := entities.Tweet{
Username: "username",
Text: "text",
}
// specify that inside usecase's CreateTweet method
// repository's CreateTweet method will be called
suite.repository.On("CreateTweet", &tweet).Return(nil)
// the real operation we need to test
err := suite.usecase.CreateTweet(&tweet)
// assertions to make sure our operation does the right thing
suite.Nil(err, "err is a nil pointer so no error in this process")
suite.repository.AssertExpectations(suite.T())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment