---
url: /runcobo/guide/testing.md
description: >-
  Test actions in-process with Crystal's spec library, against the project's
  test database.
---

# Testing

Runcobo apps are tested with Crystal's built-in [`spec`](https://crystal-lang.org/reference/guides/testing.html) library. Specs send requests straight through your actions in memory — no server, no network — while queries run against a real test database.

## Setup

`spec/spec_helper.cr` loads the app, opens a store on the test database, and defines a `dispatch` helper:

```crystal [spec/spec_helper.cr]
require "spec"
require "../src/app"

TEST_DB    = DB.open(ENV["DATABASE_URL"])
TEST_STORE = Blog::Store::Queries.new(TEST_DB)

# Start every example from empty tables.
Spec.before_each do
  TEST_DB.exec "TRUNCATE posts, authors RESTART IDENTITY CASCADE"
end

def dispatch(method : String, path : String, body : String? = nil, headers = HTTP::Headers.new,
             handler : HTTP::Handler = Blog::RouteHandler.new(TEST_STORE)) : HTTP::Client::Response
  request = HTTP::Request.new(method, path, headers, body)
  io = IO::Memory.new
  response = HTTP::Server::Response.new(io)
  handler.call(HTTP::Server::Context.new(request, response))
  response.close
  HTTP::Client::Response.from_io(io.rewind)
end

def json_headers
  HTTP::Headers{"Content-Type" => "application/json"}
end
```

`dispatch` runs a request through the same `Blog::RouteHandler` the app uses in `main.cr`, so actions get a real `store`. This works because `src/app.cr` [loads the app without starting it](/guide/project-structure#app-cr-and-main-cr).

## Writing specs

Test behavior through HTTP, the way clients use your API. Use `TEST_STORE` to set up data:

```crystal [spec/posts_spec.cr]
require "./spec_helper"

describe "POST /posts" do
  it "creates a post" do
    author = TEST_STORE.create_author(name: "Ada")

    response = dispatch("POST", "/posts", %({"author_id":#{author.id},"title":"Hello"}), json_headers)

    response.status_code.should eq(201)
    JSON.parse(response.body)["title"].as_s.should eq("Hello")
    TEST_STORE.list_posts.size.should eq(1)
  end

  it "rejects an empty title" do
    response = dispatch("POST", "/posts", %({"author_id":1,"title":""}), json_headers)

    response.status_code.should eq(422)
    error = JSON.parse(response.body)["errors"][0]
    error["path"].as_a.map(&.as_s).should eq(["title"])
    error["code"].as_s.should eq("filled")
  end
end
```

## Running specs

```sh
bin/cobo test --prepare
```

`cobo test` runs `crystal spec` with `DATABASE_URL` set to the project's local test database (`blog_test`). `--prepare` creates that database if needed and applies pending migrations first — use it after adding a migration. Pass spec arguments after the command:

```sh
bin/cobo test spec/posts_spec.cr -- --verbose
```

See [Database & Migrations](/cli/database) for the test database.

## Testing the full handler chain

`dispatch` calls your route handler directly. To include logging, error handling and the `404` handler, pass the whole chain:

```crystal
chain = HTTP::Server.build_middleware([
  Runcobo::LogHandler.new,
  Runcobo::ErrorHandler.new,
  Blog::RouteHandler.new(TEST_STORE),
  Runcobo::RouteNotFoundHandler.new,
] of HTTP::Handler)

response = dispatch("GET", "/missing", handler: chain)
response.status_code.should eq(404)
```

## Against a real server

For end-to-end tests — real sockets, keep-alive, streaming — bind a random port:

```crystal
server = Runcobo::Server.new([Blog::RouteHandler.new(TEST_STORE)] of HTTP::Handler)
address = server.instance.bind_unused_port
spawn { server.instance.listen }

response = HTTP::Client.get("http://127.0.0.1:#{address.port}/healthcheck")
response.status_code.should eq(200)

server.close
```
