Getting 'Bad value for range' in RSpec

sameera207 picture sameera207 · Jul 28, 2014 · Viewed 7.1k times · Source

Here is my code:

#app/models/user.rb
class User < ActiveRecord::Base

  def display_name
    name.split(' ').try(:first)
  end
end

#spec/models/user_spec.rb
require 'rails_helper'

RSpec.describe User, :type => :model do
  let(:user) { FactoryGirl.create :user }

  it "should return the correct display name" do
    expect ( user.display_name ).to eql("Chuck")
  end
end

#spec/factories/users.rb
FactoryGirl.define do
  factory :user do
    provider "MyString"
    uid "MyString"
    name "Chuck Norris"
    oauth_token "MyString"
    oauth_expires_at "2014-07-24 07:31:36"
  end
end 

But when I run this spec, I'm getting the following error:

Failure/Error: expect ( user.display_name ).to eql("Chuck")
     ArgumentError:
       bad value for range

and it seems like it checks the to method from rails active support, but I think to should be a method in rspec. Following is my backtrace

# /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/core_ext/string/access.rb:62:in `to'
     # ./spec/models/user_spec.rb:7:in `block (2 levels) in <top (required)>'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example.rb:148:in `instance_exec'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example.rb:148:in `block in run'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example.rb:210:in `call'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example.rb:210:in `block (2 levels) in <class:Procsy>'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-rails-3.0.2/lib/rspec/rails/adapters.rb:72:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example.rb:294:in `instance_exec'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example.rb:294:in `instance_exec'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/hooks.rb:430:in `block (2 levels) in run'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example.rb:210:in `call'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example.rb:210:in `block (2 levels) in <class:Procsy>'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/hooks.rb:432:in `run'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/hooks.rb:485:in `run'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example.rb:303:in `with_around_example_hooks'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example.rb:145:in `run'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example_group.rb:494:in `block in run_examples'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example_group.rb:490:in `map'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example_group.rb:490:in `run_examples'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/example_group.rb:457:in `run'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:112:in `block (2 levels) in run_specs'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:112:in `map'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:112:in `block in run_specs'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/reporter.rb:54:in `report'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:108:in `run_specs'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:86:in `run'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:70:in `run'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:38:in `invoke'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-3.0.3/exe/rspec:4:in `<top (required)>'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/bin/rspec:23:in `load'
     # /Users/sameera/.rvm/gems/ruby-2.0.0-p247/bin/rspec:23:in `<main>'

I checked this solution and it doesn't work.

I'm on

  • Rails 4.0.2
  • Rspec 3.0.0

Answer

sevenseacat picture sevenseacat · Jul 28, 2014

Remove the space between expect(user.display_name). You can have one inside the parentheses if you want, but not before them.