stevenspiel
stevenspiel

Reputation: 5999

Rspec match syntax

The first test passes but the other two do not. What am I doing wrong with my syntax? The only difference is match vs eq. I know I have used match before, but I can't seem to find good documentation of it online.

The error I'm getting is: undefined method 'match' for 1:Fixnum

describe Die do
    describe "new roll" do #TEST PASSES
        it "returns a number" do
            expect(Die.instance_method(:initialize).arity).to eq 1
        end
    end

    describe "new roll" do # error undefined method 'match' for 1:Fixnum
        it "returns a number" do
            expect(Die.instance_method(:initialize).arity).to match 1
        end
    end

    describe "new roll" do # error expected /\d/ got 1
        it "returns a number" do
            expect(Die.instance_method(:initialize).arity).to eq (/\d/)
        end
    end
end

Upvotes: 2

Views: 748

Answers (1)

Ash Wilson
Ash Wilson

Reputation: 24458

Your second test isn't passing because match works to compare Strings or Regexes to Strings, not Fixnums. Your third test isn't passing because the regex /\d/ is not equal to the Fixnum 1, it matches the String "1".

describe Die do
    describe "new roll" do #TEST PASSES
        it "returns a number" do
            expect(Die.instance_method(:initialize).arity).to eq(1)
        end
    end

    describe "new roll" do
        it "returns a number" do
            # 1.to_s equals the String "1" ...
            expect(Die.instance_method(:initialize).arity.to_s).to eq("1")
        end
    end

    describe "new roll" do
        it "returns a number" do
            # ... and "1" matches the regex /\d/
            expect(Die.instance_method(:initialize).arity.to_s).to match(/\d/)
        end
    end
end

Upvotes: 2

Related Questions