# RSpec With GitHub Actions

> In this short overview, see how RSpec can be incorporated into your GitHub Actions workflows.

- Canonical: https://dennisokeeffe.com/blog/2022-03-18-rspec-with-github-actions
- Published: 2022-03-18
- Updated: 2022-03-18
- Tags: ruby

GitHub Actions is a great way to run your RSpec tests in your CI/CD workflows.

This post covers how to setup RSpec testing when a push is made to a remote GitHub repo.

For an alternative example that runs RSpec tests with code coverage for PRs, see my other post ["SimpleCov With Ruby And GitHub Actions"](https://blog.dennisokeeffe.com/blog/2022-03-12-simplecov-with-ruby-and-github-actions).

Source code can be found [here](https://github.com/okeeffed/rspec-github-actions).

## Prerequisites

1. Basic familiarity with [Bundler](https://bundler.io/).
2. Familiarity with reading tests written in [RSpec](https://rspec.info/).

## Getting started

We will create the project directory `rspec-github-actions` and use Bundler to initialize the project:

```s
$ mkdir rspec-github-actions
$ cd rspec-github-actions

# initialise Bundler project
$ bundle init
# RSpec added for testing
$ bundle add rspec --group "development,test"

# Add other required files
$ mkdir lib spec
$ touch lib/contrived_math.rb spec/math_spec.rb

# Add the GitHub workflow folder
$ mkdir -p .github/workflows
$ touch .github/workflows/rspec.yml
```

In the above, we do the following:

1. Initialize the project with Bundler.
2. Add in the RSpec gem.
3. Make some folders and files for our code, tests and GitHub workflows.

In our example, we are creating some contrived math functions to test against.

## Writing our math code

Inside of `lib/contrived_math.rb`, add the following:

```rb
module ContrivedMath
  def self.add(a, b)
    a + b
  end

  def self.subtract(a, b)
    a - b
  end
end
```

The above adds (unnecessary) functions for both addition and subtraction.

These will be the functions that we write tests for.

## Our tests

Inside of `spec/math_spec.rb`, add the following code:

```rb
require 'contrived_math'

RSpec.describe Math do
  describe '#add' do
    it 'adds two numbers' do
      expect(ContrivedMath.add(1, 2)).to eq(3)
    end
  end

  describe '#subtract' do
    it 'subtracts two numbers' do
      expect(ContrivedMath.subtract(2, 1)).to eq(1)
    end
  end
end
```

At this point we can run our tests with `bundle exec rspec` and see our tests are passing.

## Writing the GitHub Action

Inside of `.github/workflows/rspec.yml`:

```yml
name: Run RSpec tests
on: [push]
jobs:
  run-rspec-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          # Not needed with a .ruby-version file
          ruby-version: 2.7
          # runs 'bundle install' and caches installed gems automatically
          bundler-cache: true
      - name: Run tests
        run: |
          bundle exec rspec
```

The above YAML configuration will tell GitHub to run an action on each push to remote where our job `run-rspec-tests` will outline the steps required to run the RSpec tests.

## Testing our action

At this point, all we need to do is push to the remote repo and see everything in action.

> Note: I am expecting that you have initialized your Git repo at this point, then add all the files and set up remote.

Once you push your code to remote, checkout the Git repo and under the actions tab you will see your job running:

![Actions tab](https://cdn.dennisokeeffe.com/assets/2022-03-18-rspec-with-github-actions/1-test-running.webp)

Click into the action and let it run, and once there is a success you can see the output in the job.

![Successful job](https://cdn.dennisokeeffe.com/assets/2022-03-18-rspec-with-github-actions/2-test-completed.webp)

> Note: I ran the job a second time (which is displayed in the screenshot above). The images have job #1 and #2 respectively but the result is the same.

## Summary

Today's post demonstrated how to implement GitHub Actions with your RSpec tests.

## Resources and further reading

- [Bundler](https://bundler.io/)
- [RSpec](https://rspec.info/)
- [GitHub Actions](https://github.com/features/actions)
- [Source code](https://github.com/okeeffed/rspec-github-actions)
- ["SimpleCov With Ruby And GitHub Actions"](https://blog.dennisokeeffe.com/blog/2022-03-12-simplecov-with-ruby-and-github-actions)

_Photo credit: [`pawel_czerwinski`](https://unsplash.com/@pawel_czerwinski)_
