You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
5 Hrs
5 Min
23 Seg

Corriendo nuestras pruebas

9/30
Resources

How to work with GitHub Actions for code validation?

The advancement of software development requires not only writing efficient code, but also automating tasks that can optimize the workflow. GitHub Actions is a powerful tool that allows you to set up automatic flows, such as code validation. In this context, I will guide you through the process of making a commit, observing its behavior on GitHub and troubleshooting possible errors.

How to make an effective commit?

  1. Prepare changes: Before committing, make sure you have reviewed and saved all changes to your code.
  2. Add changes: Use the git add command to add all the changes made to the files in the repository.
  3. Name the commit descriptively: A good commit name makes it easy to read and review the history. For example: git commit -m "Add GitHub Actions Config".
  4. Push the changes: Finally, push the changes to the remote repository using git push.

What to do if the actions fail?

When integrating GitHub Actions, it is common to encounter errors in the executed workflows. Suppose you see an error message like "Unable to Resolve Action". Here are some steps to fix it:

  • Check the error message: read carefully the message provided by GitHub. In this case, it may be a syntax error.
  • Check the YAML file: Correct items such as incorrect versions. For example, change from Checkout @4 to Checkout v4.
  • Upload the changes and verify: Once corrected, perform a new push. GitHub will re-run the actions where you can verify the fix.

How do I set up unit tests with mocks in Go?

An essential part of any development is to ensure that our code works as expected. For this, unit tests are fundamental. Below, I will explain how to set up a unit test using mocks.

How to start a unit test?

  1. Create a new test: Open the corresponding test file, such as main_test.go, and create a new test method.
  2. Set the environment: Initialize the values your method will need, such as context, repositories, webhooks, and message bodies. Be sure to prepare a Dummy object and convert it to JSON when necessary.
  3. Manage time: Generate the created time using time.Now() to simulate the exact time of creation.

How to use mocks correctly?

  • Initialize the mock object: Use the mock library to create an object that simulates the behavior of external dependencies.
  • Specify expected calls: Define the method call to be tested, such as Insert. Be sure to define what arguments it will receive and what the expected behavior is.
  • Perform the call and verify: Call the method you want to test and use assert to verify that there are no errors and that the expectations of the mock are met.

Example of test code in Go

func TestInsert(t *testing.T) { c := new(YourTestingFramework) webhook := &models.Webhook{ Repository: models.Repository{FullName: "example/repo"}, HeadCommit: models.HeadCommit{ ID: "commit123", Message: "This is a commit", Author: models.Author{ Username: "user1", Email: "[email protected]", }, }, }, }
 body, err := json.Marshal(webhook) assert.NoError(t, err)
 createdTime := time.Now()
 mockRepo := new(YourMockRepo) mockRepo.On("Insert", mock.Anything, mock.Anything).Return(nil)
 err = InsertGitHubWebhook(context.Background(), mockRepo, webhook, body, createdTime) assert.NoError(t, err) mockRepo.AssertExpectations(t)}

This snippet is just an example, be sure to adapt it to your particular needs.

How to ensure that GitHub Actions runs unit tests?

Finally, to successfully integrate with GitHub Actions and have it run your tests on every commit:

  • Upload your test changes: commit your tests and push them to your repository.
  • Verify the execution: In the GitHub interface, monitor the status of your tests. A yellow dot indicates that they are running and a green check that they finished successfully.
  • Review the log details: Open the actions to see exactly which tests were executed and their outcome.

With the proper use of GitHub Actions and automated unit tests, we will not only improve code quality but also optimize our development process by quickly identifying potential bugs or bottlenecks in the workflow. Stay ahead in your learning process!

Contributions 3

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Ud es la 2ra persona quien usa un VSC LIGHTWHITE Y VER eso me genera extremo Fatiga Visual y Ansiedad porque la clases termine ASAP, creo que @PlatziTeam debio hacerle cambiar el color tan Intenso al menos por este curso .
```txt webhook := models.GitHubWebhook{ Repository: models.Repository{ FullName: "camilaleniss/secure-dev", }, HeadCommit: models.Commit{ ID: "9da3ed5d641d46dd1401d0768bc9dde90e86e1cb", Message: "Add sample code for handle-github-webhook", Author: models.CommitUser{ Email: "[email protected]", Username: "camilaleniss", }, }, } ```
Es buen铆simo.