Руководство по RoR Глава 6 продолжает получать синтаксическую ошибку, неожиданный tIDENTIFIER, ожидающий ключевое слово_end (SyntaxError

Я продолжаю получать следующую ошибку:

syntax error, unexpected tIDENTIFIER, expecting keyword_end (SyntaxError)

    from /Users/shazias/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in `block in load'
        from /Users/shazias/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
        from /Users/shazias/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in `load'
        from /Users/shazias/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `block in load_spec_files'
        from /Users/shazias/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `map'
        from /Users/shazias/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load_spec_files'
        from /Users/shazias/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22:in `run'
        from /Users/shazias/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69:in `run'
        from /Users/shazias/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'

вот мой файл user.rb:

    class User < ActiveRecord::Base
    attr_accessible :name, :email

    email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

   validates :name,  :presence => true,
                    :length   => { :maximum => 50 }
   validates :email, :presence => true,
                    :format   => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false}
   end

    and my user_spec.rb file: 

       require 'spec_helper'

    describe User do



before(:each) do
    @attr = { :name => "Example User", :email => "user@example.com" }
  end

  it "should create a new instance given valid attributes" do
    User.create!(@attr)
  end

  it "should require a name" do
      no_name_user = User.new(@attr.merge(:name => ""))
      no_name_user.should_not be_valid
  end

  it "should require an email address" do
        no_email_user = User.new(@attr.merge(:email => ""))
        no_email_user.should_not be_valid
  end

  it "should reject names that are too long" do
          long_name = "a" * 51
          long_name_user = User.new(@attr.merge(:name => long_name))
          long_name_user.should_not be_valid
  end

  it "should accept valid email addresses" do
            addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
            addresses.each do |address|
              valid_email_user = User.new(@attr.merge(:email => address))
              valid_email_user.should be_valid
  end
 end 
  it "should reject invalid email addresses" do
              addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
              addresses.each do |address|
                invalid_email_user = User.new(@attr.merge(:email => address))
                invalid_email_user.should_not be_valid
  end
 end
  it "should reject duplicate email addresses" do
    #Put a user with given email address into the database. 
    User.create! (@attr)
    user_with_duplicate_email = User.new (@attr)
    user_with_duplicate_email.should_not be_valid
  end
end
  it "should reject email addresses identical up to case" do
    upcased_email = @attr [:email].upcase
    User.create! (@attr.merge(:email => upcased_email))
    user_with_duplicate_email = User.new (@attr),
    user_with_duplicate_email.should_not be_valid
  end
end

Я буквально нахожусь в тупике.. Мой тест продолжает проваливаться после того, как я добавляю эту часть кода в файл user_spec.rb (перед добавлением и / или удалением этого раздела кода мои тесты проходят):

    it "should reject email addresses identical up to case" do
    upcased_email = @attr [:email].upcase
    User.create! (@attr.merge(:email => upcased_email))
    user_with_duplicate_email = User.new (@attr),
    user_with_duplicate_email.should_not be_valid
  end
end

Любая помощь с благодарностью, спасибо!

3 ответа

У вас есть много дополнительных ENDs...

Когда вы открываете do, лайк it "should something" do вам нужно закрыть открытое do с end:

it "should something" do
  ...
end

Однако в некоторых случаях вы закрываете его дважды:

it "should something" do
  ...
end
end

Ваш исправленный код должен быть:

require 'spec_helper'

describe User do
  before(:each) do
  @attr = { :name => "Example User", :email => "user@example.com" }
end

it "should create a new instance given valid attributes" do
  User.create!(@attr)
end

it "should require a name" do
  no_name_user = User.new(@attr.merge(:name => ""))
  no_name_user.should_not be_valid
end

it "should require an email address" do
  no_email_user = User.new(@attr.merge(:email => ""))
  no_email_user.should_not be_valid
end

it "should reject names that are too long" do
  long_name = "a" * 51
  long_name_user = User.new(@attr.merge(:name => long_name))
   long_name_user.should_not be_valid
end

it "should accept valid email addresses" do
  addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
  addresses.each do |address|
  valid_email_user = User.new(@attr.merge(:email => address))
  valid_email_user.should be_valid
end

it "should reject invalid email addresses" do
  addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
  addresses.each do |address|
  invalid_email_user = User.new(@attr.merge(:email => address))
  invalid_email_user.should_not be_valid
end
it "should reject duplicate email addresses" do
  #Put a user with given email address into the database. 
  User.create! (@attr)
  user_with_duplicate_email = User.new (@attr)
  user_with_duplicate_email.should_not be_valid
end

it "should reject email addresses identical up to case" do
  upcased_email = @attr [:email].upcase
  User.create! (@attr.merge(:email => upcased_email))
  user_with_duplicate_email = User.new (@attr),
  user_with_duplicate_email.should_not be_valid
end

Прямо над этой линией:

it "should reject email addresses identical up to case" do

У вас есть дополнительный end заявление там. Это утверждение, вероятно, следует перенести в конец файла.

Вы используете ruby ​​1.9.3

в вашей проверке замените: => на :

validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
Другие вопросы по тегам