16

I'm having problems getting this simple test to pass on RSpec 2.8.

I want to write a simple test for the absence of parameters on a method that requires them (i.e. ArgumentError: wrong number of arguments ('x' for 'y')).

My test is testing a Gem module method like so:

describe "#ip_lookup" do
  it "should raise an ArgumentError error if no parameters passed" do
    expect(@geolocater.ip_lookup).to raise_error(ArgumentError)
  end
end

My gem module code looks like this:

module Geolocater
  def ip_lookup(ip_address)
   return ip_address
  end
end

My spec runs with this output.

Failure/Error: expect(@geolocater.ip_lookup).to raise_error(ArgumentError)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # ./lib/geolocater.rb:4:in `ip_lookup'
     # ./spec/geolocater_spec.rb:28:in `block (3 levels) in <top (required)>'

What am I missing here?

1 Answer 1

37

You need to pass a block to #expect, not a regular argument:

describe "#ip_lookup" do
  it "should raise an ArgumentError error if no parameters passed" do
    expect { @geolocater.ip_lookup }.to raise_error(ArgumentError)
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.