Getting 'Bad value for range' in RSpec
Asked Answered
H

3

9

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
Hanuman answered 28/7, 2014 at 1:0 Comment(0)
L
32

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

Legislatorial answered 28/7, 2014 at 4:4 Comment(2)
Why is this so?Ransome
As far as I can tell, it interprets the line like expect((user.display_name ).to eql("Chuck")). The .to binds more tightly than the expect(), when the space is added, which isn't what you want - you want to call expect with the argument user.display_name, then call to on the result of that.Legislatorial
O
1

Can you try assigning the value to a var first, like this:

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

  it "should return the correct display name" do
    user_display_name = user.display_name
    expect (user_display_name).to eql("Chuck")
  end
end
Orator answered 28/7, 2014 at 4:1 Comment(1)
thanks for the answer, but it didnt work however the one gave by @Legislatorial worked :)Hanuman
D
0

Use:

  def display_name
    name.split(' ')[0]
  end

Or better:

  def display_name
    name.blank? ? "" : name.split(' ')[0]
  end
Dasie answered 28/7, 2014 at 1:22 Comment(1)
thanks , but this method is correct and the error is not in method itself, Coz this works in the command lineHanuman

© 2022 - 2024 — McMap. All rights reserved.