这是我的代码:
#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
但是当我运行此规范时,我收到以下错误:
Failure/Error: expect ( user.display_name ).to eql("Chuck")
ArgumentError:
bad value for range
似乎它会从rails to
检查active support
方法,但我认为to
应该是rspec
中的方法。以下是我的回溯
# /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>'
我查了this solution但它没有用。
我
答案 0 :(得分:28)
删除expect(user.display_name)
之间的空格。如果你愿意,你可以在括号内有一个,但不能在它们之前。
答案 1 :(得分:1)
您可以先尝试将值分配给var,如下所示:
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
答案 2 :(得分:0)
使用:
def display_name
name.split(' ')[0]
end
或更好:
def display_name
name.blank? ? "" : name.split(' ')[0]
end