icy-arctic-fox / spectator

Feature-rich testing framework for Crystal inspired by RSpec.
https://gitlab.com/arctic-fox/spectator
MIT License
101 stars 4 forks source link

expect().to be === does not actually call subject#=== #26

Closed postmodern closed 3 years ago

postmodern commented 3 years ago

Ran into this when testing custom #=== methods.

Example Code

module Test
  VERSION = "0.1.0"

  class Divisible

    def initialize(@div : Int32)
    end

    def ===(other : Int32)
      (other % @div) == 0
    end

  end
end
require "./spec_helper"
require "../src/test"

Spectator.describe Test::Divisible do
  let(i) { 3 }
  subject { described_class.new(i) }

  describe "#===" do
    it "must call === and compare the other object" do
      expect(subject).to be === 6
    end
  end
end

Output

$ shards install
Resolving dependencies
Fetching https://gitlab.com/arctic-fox/spectator.git
Using spectator (0.9.36)
$ crystal spec
Test::Divisible
  #===
    must call === and compare the other object

Failures:

  1) Test::Divisible#=== must call === and compare the other object
     Failure: subject does not equal 6

         actual: #<Test::Divisible:0x7f2fbab6abd0 @div=3>
       expected: 6

     # spec/test_spec.cr:9

Finished in 341 microseconds
1 examples, 1 failures, 0 errors, 0 pending

Workaround

However, if we explicitly call subject#=== within expect(), it works:

    it "must call === and compare the other object" do
      expect(subject === 6).to be(true)
    end
$ crystal spec
Test::Divisible
  #===
    must call === and compare the other object

Finished in 104 microseconds
1 examples, 0 failures, 0 errors, 0 pending
icy-arctic-fox commented 3 years ago

Looks like Spectator is missing some be variants that RSpec has. The docs don't show be ===, but the code does. I'll look into getting this added soon.

icy-arctic-fox commented 3 years ago

Sorry for the delay. Should be fixed in the latest version - 0.9.37.

postmodern commented 3 years ago

Seems to be working now. Thanks!