Error messages in spec

I am witing a REST API. I use EdgeDB as database, which doesn’t have Crystal bindings yet. However, instead of 2 dimensional tables, it works with objects that can be simply deserialized from JSON. This is good, until accidentally I try to deserialize a different object than I wrote in the query, because currently there isn’t a type safe query builder.
I want to do test driven development, however, unit testing doesn’t quite fits the project, because there aren’t so much separately testable units. End-to-end testing would be a viable solution.
I can not monkey patch the database adapter, because there is a lot of logic and checks happening in EdgeDB space, and if I didn’t use the real database for the tests, it could produce hugely different results.
Is it possible to print error messages in crystal spec? I tried Log, but it doesn’t work in spec mode. I could use puts or p, but that’s not quite elegant. I want to only emit error messages for the failed tests, if it’s possible.

There is fail method which you can use to output error message of your preference. It can be used like

it "some test case" do
  fail "This is error message" if 1 == 2

Conditions can be anything which you would like to validate.


Can I use this with e.g. response.status_code.should eq(200), or I have to do manual checking?
Is it possible to programmatically react to the result of #should ?

Object extensions like should etc comes with failure_message param, which you can use to pass message of your choice.

  it "test custom error message" do
    1.should eq(2), failure_message: "Oh nooo one is not two"

Above test will print Oh nooo one is not two instead of default message.


Thanks. This is exactly what I was looking for.

FWIW, if you want log message in crystal spec you can use LOG_LEVEL=info crystal spec, but I agree that having expectations is the way to go in this case.