Not sure why simple test throws exception

Hi,

I have been away from Crystal for a couple of month and today I decided to make a new app in Crystal.

I have this stupid simple test where I assert that the given Hash is not empty.

it "should not be empty" do
  hash = Foo.bar # => Hash(String, String)

  hash.empty?.should be_false
end

This I would expect to just fail with some red text saying expected: this - got: this - I have not implemented the method yet, all I have at this point is

module Foo
  def self.bar : Hash(String, String)
    Hash(String, String).new
  end
end

The test, however, says that everything passes and the throws an exception.

example

Finished in 48 microseconds
0 examples, 0 failures, 0 errors, 0 pending
Unhandled exception: Expected: false
     got: true (Spec::AssertionFailed)
  from /usr/lib/crystal/spec/methods.cr:82:5 in 'fail'
  from /usr/lib/crystal/spec/expectations.cr:454:9 in 'should'
  from eval:5:1 in '__crystal_main'
  from /usr/lib/crystal/crystal/main.cr:115:5 in 'main_user_code'
  from /usr/lib/crystal/crystal/main.cr:101:7 in 'main'
  from /usr/lib/crystal/crystal/main.cr:127:3 in 'main'
  from /usr/lib/libc.so.6 in '__libc_start_main'
  from /home/crystal/.cache/crystal/crystal-run-eval.tmp in '_start'
  from ???

Anyone that has an idea why this throws an exception?

Your playground link isn’t the same as what you provided in the two code example. The gist of it is the Spec module is raising exceptions, which are normally caught within an it block, but since your assertion isn’t wrapped in one, it bubbles up.

https://play.crystal-lang.org/#/r/coh0

2 Likes

Ah I see! I just checked my own tests again and I see that my test was wrapped in a describe instead of an it block.