Ruby-on-rails 语法错误“语法错误,意外的输入结束,需要keyword_end(SyntaxError)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18721178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error "syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)"
提问by HandDisco
I can't run my Rspec user_spec.rb test due to a syntax error. Too many "end" perhaps in 2 different files? I've added and deleted 'end' in certain places without success.
由于语法错误,我无法运行 Rspec user_spec.rb 测试。可能在 2 个不同的文件中有太多“结束”?我在某些地方添加和删除了“结束”,但没有成功。
Syntax error "syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)" require 'spec_helper'
语法错误“语法错误,意外的输入结束,期望关键字结束(SyntaxError)”需要'spec_helper'
user_spec.rb
用户规范.rb
describe User do
before do
@user = User.new(name: "Example User", email: "[email protected]",
password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should be_valid }
before do
@user = User.new(name: "Example User", email: "[email protected]")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
before do
@user = User.new(name: "Example User", email: "[email protected]")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should be_valid }
describe "when name is too long" do
before { @user.name = "a" * 51 }
it { should_not be_valid }
end
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.
foo@bar_baz.com foo@bar+baz.com]
addresses.each do |invalid_address|
@user.email = invalid_address
expect(@user).not_to be_valid
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[[email protected] [email protected] [email protected] [email protected]]
addresses.each do |valid_address|
@user.email = valid_address
expect(@user).to be_valid
end
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.save
end
describe "when password is not present" do
before do
@user = User.new(name: "Example User", email: "[email protected]",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { @user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
user_pages_spec.rb
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
end
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "[email protected]"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
Terminal Output
终端输出
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load': /Users/Abraham/code/sample_app/spec/models/user_spec.rb:85: syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)
from /usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
回答by Miguelgraz
It seems that you have a bunch of describes that never have ends keywords, starting with describe "when email format is invalid" dountil describe "when email address is already taken" do
似乎你有一堆describe从来没有ends 关键字的 s,从describe "when email format is invalid" do直到describe "when email address is already taken" do
Put an end on those guys and you're probably done =)
结束那些家伙,你可能就完了 =)
回答by Jason Carty
Do you perhaps have one too many here?
你这里有太多了吗?
describe "when name is too long" do
before { @user.name = "a" * 51 }
it { should_not be_valid }
end
end
回答by Dani
$ rails server -b $IP -p $PORT - that solved the same problem for me
$ rails server -b $IP -p $PORT - 为我解决了同样的问题

