0

我不断收到以下错误:

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

非常感谢任何帮助,谢谢!

4

3 回答 3

1

Directly above this line:

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

You have an extra end statement in there. That statement should probably be moved to the end of the file.

于 2012-06-24T19:56:47.353 回答
1

你使用 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 }
于 2012-06-24T20:14:30.737 回答
1

你有很多额外的ENDs...

当您打开 ado时,就像it "should something" do您需要用 a 关闭打开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
于 2012-06-24T21:42:57.320 回答