# Write Minitest Tests for Scheduled Rake Tasks in Rails

Yes. An agent loads the app's rake tasks in Minitest, invokes each scheduled task against fixtures, asserts the jobs it enqueues and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/write-rake-task-tests/minitest/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent looks for the code that runs on a timer and has no test. In a Rails app like codetriage/CodeTriage, that is `lib/tasks/schedule.rake`: a `schedule` namespace whose tasks an external scheduler starts, from `triage_emails` and `populate_issues` to `poke_inactive`, `warn_invalid_token` and `fetch_labels_and_assign`. The suite under `test/` is Minitest with `ActiveSupport::TestCase`, fixtures loaded with `fixtures :all`, WebMock and VCR with `allow_http_connections_when_no_cassette = false`, and job tests in `test/jobs` that inherit from `ActiveJob::TestCase`. No file in `test/` loads a rake task, so these tasks run in production without ever having run in CI.

It writes `test/tasks/schedule_rake_test.rb` as an `ActiveJob::TestCase`. A setup step calls `Rails.application.load_tasks` once, guarded by `Rake::Task.task_defined?("schedule:triage_emails")` so tasks are not defined twice, and a helper reenables a task before each `invoke`, because Rake runs a task once per process otherwise. Each test then asserts what the task enqueues against the fixture data in `test/fixtures/users.yml`, `repos.yml` and `repo_subscriptions.yml`: `triage_emails` enqueues one `SendDailyTriageEmailJob` per user returned by `User.with_repo_subscriptions`, `populate_issues` one `PopulateIssuesJob` per repository in `Repo.active`, and an archived repository gets none.

The day-of-week tasks are tested with time travel. `poke_inactive` returns early unless `Date.today.tuesday?`, `warn_invalid_token` unless it is Thursday, and `sitemap` unless it is Sunday, so the agent uses `travel_to` with a known Tuesday and a known Wednesday and asserts both that `BackgroundInactiveEmailJob` is enqueued with the percentile arguments and that nothing is enqueued on the wrong day. `poke_inactive` runs `PERCENTILE_CONT(0.90)` against Postgres directly, which is a reason these tests use the real test database and not stubs. `mark_closed` gets a test that ages an open issue past 24 hours with `update_column` and asserts it is closed.

Tasks that call GitHub are pinned with the tools the suite already uses. `update_repos` raises `GITHUB API APPEARS TO BE DOWN` when `github_api_up?` fails, so one test stubs the rails/rails repository request with WebMock to return a 500 and asserts the raise, and another returns a 200 with a small JSON body and asserts one `UpdateRepoInfoJob` per row in `repos`, since this task walks `Repo.find_each` and not only active repositories. `check_user_auth` is a `next` with the rest commented out, and the agent leaves it untested and says so. To run it, the agent installs the Ruby that `.ruby-version` names and PostgreSQL, copies `config/database.ci.yml`, loads the schema and runs `bin/rails test test/tasks` and then the full `bin/rake test` plus `bundle exec standardrb`, as `.github/workflows/main.yml` does.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Write Minitest tests for every task in lib/tasks/schedule.rake. Read the rake file, test/test_helper.rb, test/fixtures, test/jobs and .github/workflows/main.yml first. Add test/tasks/schedule_rake_test.rb as an ActiveJob::TestCase that loads the tasks once, reenables each before invoking it, and asserts the jobs it enqueues against the fixtures. Use travel_to for the day-of-week tasks and test both the right and the wrong day. Stub GitHub with WebMock or the existing VCR cassettes. Do not change lib/tasks. Run bin/rake test and bundle exec standardrb and open a pull request with the output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds `test/tasks/schedule_rake_test.rb` and nothing under `lib/` or `app/`. The description lists each task in `schedule.rake` with the test that covers it and what it asserts, and names the ones left out on purpose: `check_user_auth`, which does nothing today, and `sitemap`, whose real work is the `sitemap:refresh` task from the sitemap gem.

It includes the `bin/rake test` and `standardrb` output from the sandbox, and the repository's `Main` workflow runs the same steps against its Postgres 16 service on the branch. If a test exposes a bug in a task, for example a scope that enqueues jobs for archived repositories, the agent writes the failing test, marks it skipped with a note, and describes the bug in the pull request instead of fixing the task in the same change.

A reviewer checks that the fixture counts the tests rely on are the ones the team expects to keep, since a new user fixture with a subscription changes the expected number of `SendDailyTriageEmailJob` calls, and that the day-of-week dates read correctly in the server's timezone.

## Run it on every pull request labelled needs-tests

Once these tests exist, CI catches a broken task on every pull request, and the agent is for the next task nobody tested. An automation in the Project with a `github` trigger on `pull_request.labeled` and `needs-tests` in its `labels` adds the pull request after the prompt, so the prompt says to read the rake or job files the pull request that follows changes, write the missing tests on its branch and push them there.

From your own tooling, the same work is one `POST /o/{org}/sessions` with this body and the repository in `resources`. A key with a webhook URL gets `run.completed` with the `session_url` when the turn ends, and the pull request link is on the session.

## Why run it on Rush?

Scheduled tasks are the code that fails at 3 a.m. with nobody watching, and testing them needs the real pieces: Postgres for `PERCENTILE_CONT`, the fixtures, the Active Job test adapter and the GitHub cassettes. The sandbox installs them and runs the whole suite, and each command is kept in the run's record with its output, so the reviewer sees the tests pass in an environment shaped like CI.

The work is narrow and easy to check, which suits an agent: one new file, no production code, and a clear list of what each test asserts. Several apps with untested rake tasks can each get a run at the same time, and each pull request stands on its own.

## Frequently asked questions

### Why reenable a task before every invoke?

Rake marks a task as already run after its first `invoke` in a process, and a second call does nothing. Calling `reenable` resets that flag, so each test runs the task body again instead of passing silently.

### Will loading the rake tasks slow the suite?

`Rails.application.load_tasks` reads every file in `lib/tasks` and the tasks that gems define, and the test file loads them once per process behind a guard. The other test files do not load them at all.

### Does it test the scheduler itself?

No. The times are set in the scheduler outside the repository. The tests prove what each task does when it starts, which is the part the repository owns.

### What if our app uses RSpec instead?

The same approach works with `Rake::Task` and `have_enqueued_job` in a spec under `spec/tasks`. The agent follows whichever framework the repository already runs and does not add a second one.

## More jobs like this: Write the missing tests

- [Write RSpec Tests for a Rails App](https://getrush.ai/run/write-rspec-tests/rails/)
- [Write Integration Tests for an Axum API](https://getrush.ai/run/write-rust-integration-tests/axum/)
- [Write Tests for a Rocket Server](https://getrush.ai/run/write-rust-tests/rocket/)
- [Write Service Tests for a Feathers App](https://getrush.ai/run/write-service-tests/feathers/)
- [Write Snapshot Tests for a Textual App](https://getrush.ai/run/write-snapshot-tests/textual/)
- [Write Tests for Scrapy Spiders](https://getrush.ai/run/write-spider-tests/scrapy/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
