Kemal helpers to Crystal's spec for easy testing.
Add it to your shard.yml.
name: your-kemal-app
version: 0.1.0
dependencies:
spec-kemal:
github: kemalcr/spec-kemal
branch: master
kemal:
github: kemalcr/kemal
branch: masterJust require it before your files in your spec/spec_helper.cr
require "spec-kemal"
require "../src/your-kemal-app"Your Kemal application
# src/your-kemal-app.cr
require "kemal"
get "/" do
"Hello World!"
end
Kemal.runNow you can easily test your Kemal application in your specs.
KEMAL_ENV=test crystal spec
# spec/your-kemal-app-spec.cr
describe "Your::Kemal::App" do
# You can use get,post,put,patch,delete to call the corresponding route.
it "renders /" do
get "/"
response.body.should eq "Hello World!"
end
endIf you are using Kemal Session in your application it's possible to test session related features as well.
To do that require "spec-kemal/session" instead of just spec-kemal, then
you can use with_session to create a session for your requests.
require "spec-kemal/session"
it "works with sessions" do
with_session do |session|
session.bigint("user_id", 12345) # sets a session value
get "/dashboard"
response.body.should eq "session value"
end
endErrors gets rescued by default which results in the Kemal's exception page is rendered.
This may not always be the desired behaviour, e.g. when a JSON parsing error occurs one might expect "[]"
and not Kemal's exception page.
Set Kemal.config.always_rescue = false to prevent this behaviour and raise errors instead.
- Fork it ( https://github.com/kemalcr/spec-kemal/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- sdogruyol Sdogruyol - creator, maintainer