Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec test ArgumentError on method with parameters

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?

like image 316
thoughtpunch Avatar asked Jan 11 '12 15:01

thoughtpunch


1 Answers

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
like image 185
John Avatar answered Oct 21 '22 07:10

John