运行Feature文件的RSpec时出现语法错误:unexpected end-of-input
解决RSpec Feature测试中的
syntax error, unexpected end-of-input, expecting keyword_end错误 我帮你检查了这段RSpec测试代码,问题很明确——代码块的end不匹配,缺少了两个闭合的end语句。
问题原因
Ruby是依赖do/end划分代码块的语言,每个用do开启的代码块都必须有对应的end来闭合。你的代码里:
- 最外层的
RSpec.feature "testCreateVideo", :type => :feature do没有对应的闭合end - 中间的
context 'create new video 'do也没有对应的闭合end
写完第二个scenario的逻辑后就直接结束了文件,导致Ruby解析器读到文件末尾时,还在寻找这两个代码块的闭合标记,所以抛出了这个语法错误。
修正后的完整代码
只需要在文件末尾添加两个end,分别闭合context和RSpec.feature代码块即可:
require_relative 'rails_helper' require 'spec_helper' RSpec.feature "testCreateVideo", :type => :feature do context 'create new video 'do scenario "Successfully creates a new video" do visit "https://SomeURL/orders" within('form') do fill_in "order[organisation][name]", :with => "SAMPLE BRAND NAME" fill_in "order[organisation][website]", :with => "SAMPLE WEBSITE" select "option", from: "select box", :with => "Singapore" select "option", from: "select box", :with => "Central" choose('order[project][name]', option: 'time_frame_next_week') choose('order[project][budget]', option: 'up-to-five') fill_in "#order_project_description", with: 'sample description' fill_in "order[project][videos_we_like_tmp]", :with => "My Widget" fill_in "order[contact][first_name]", :with => "John Christian" fill_in "order[contact][last_name]", :with => "Dela Pena" fill_in "order[contact][email]", :with => "christianmamac08@gmail.com" fill_in "order[contact][mobile]", :with => "65 8134 9249" check('accepts_terms') end click_button ('button') expect(page).to have_text("Thank you for creating your quote.") end scenario "should fail creating video" do visit "https://someURL/orders" within('form') do #fill_in "order[organisation][name]", :with => "SAMPLE BRAND NAME" fill_in "order[organisation][website]", :with => "SAMPLE WEBSITE" select "option", from: "select box", :with => "Singapore" select "option", from: "select box", :with => "Central" choose('order[project][name]', option: 'time_frame_next_week') choose('order[project][budget]', option: 'up-to-five') fill_in "#order_project_description", with: 'sample description' fill_in "order[project][videos_we_like_tmp]", :with => "My Widget" fill_in "order[contact][first_name]", :with => "John Christian" fill_in "order[contact][last_name]", :with => "Dela Pena" fill_in "order[contact][email]", :with => "christianmamac08@gmail.com" fill_in "order[contact][mobile]", :with => "65 8134 9249" check('accepts_terms') end click_button ('button') expect(page).to have_text("Brand Name is missing.") end end # 闭合context代码块 end # 闭合RSpec.feature代码块
小提示
写Ruby或者RSpec代码时,建议用编辑器的代码折叠功能或者严格的缩进对齐来检查do和end的配对情况,这样能快速发现这类遗漏闭合标记的问题,避免浪费时间排查。
内容的提问来源于stack exchange,提问作者Christian Mamac
相关产品推荐
相关产品推荐

