Question

I'm stumped as to how to compare strings held in variables (or returned from methods) in RSpec.

describe "comparing strings" do 
  it "will compare with a variable" do
    a = "00001000"
    expect ( a ).to eq "00001000"
  end

  it "it will compare without a variable" do
    expect( "00001000" ).to eq "00001000" 
  end
end

leads to :

comparing strings
  will compare with a variable (FAILED - 1)
  it will compare without a variable

Failures:

  1) comparing strings will compare with a variable
     Failure/Error: expect ( a ).to eq "00001000"
     NoMethodError:
       undefined method `to' for "00001000":String
     # ./run.rb:21:in `block (2 levels) in <top (required)>'

Finished in 0.00074 seconds
2 examples, 1 failure

Why does the first example fail, but the second pass?

Était-ce utile?

La solution

You placed space after expect, you shouldn't:

expect( a ).to eq '00001000'

with space it's equivalent to:

expect a.to eq '00001000'

so to method is called on a and raises NoMethodError.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top