syntax error, unexpected $end, expecting keyword_end - ruby

I am getting this error when trying to click a button with umlaut:
syntax error, unexpected $end, expecting keyword_end
click_on 'Neue Firma hinzufц╪gen'
I am testing with Ruby & Capabara.
##Create_User_spec.rb
require 'acceptance/acceptance_helper'
## Feature 'Create User'
feature 'Create User' do ##
Scenario 'Create a User'
scenario 'Create a User' do
## Login into the service
visit 'url'
fill_in 'User-username', :with => 'test'
fill_in 'User-password', :with => 'test'
click_on 'login'
click_link 'Test'
click_on 'Neue Firma hinzufügen'
end
end

This also can happen if you have a stray . trailing a method, so check for those as well.

It happened to me because of special characters, in my case portuguese signs. I believe the problem is the "ü" in hinzufügen. Looking for a solution yet.
Edit: found a solution!
I added the following to the very top of the rb file:
# encoding: utf-8
(don't miss the # sign, it is needed)

This error due to an extra end.Mean you have written an extra end with no matching do.

This happened to me as well but because I was missing an end. I am following this tutorial
http://tutorials.jumpstartlab.com/projects/blogger.html
My model was:
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
It needed to be:
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
end
Hope that helps someone.

Related

RubyMIne capybara example from book does not work - syntax error

When running an example from the book "Application testing with Capybara", I got the error
step_definitions/steps.rb:5: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
... :with => search_term click_on 'search-btn'
... ^
My steps.rb looks like this:
Given(/^I am on the YouTube home page$/) do
visit 'http://www.youtube.com'
end
When(/^I search for "(.*?)"$/) do
|search_term| fill_in 'search_query', :with => search_term click_on 'search-btn'
end
Then(/^videos of large rodents are returned$/) do
page.should have_content 'Largest Rodent'
end
now I have new problem
according to:
betterspecs .org/#expect
I changed "then line" for..
Then(/^videos of large rodents are returned$/) do
expect(page).to have_content 'Largest rodents'
And I got:
Spec::Expectations::ExpectationNotMetError: expected to find text "Largest rodents" in "Remind me later Review A privacy reminder from YouTube, a Google company PL UploadSign in Search Home Trending BEST OF YOUTUBE
./features/step_definitions/steps.rb:10:in `/^videos of large rodents are returned$/'
./features/youtube_search.feature:5:in `Then videos of large rodents are returned'
fill_in and click_on are two distinct methods. Your code should look like this:
When(/^I search for "(.*?)"$/) do |search_term|
fill_in 'search_query', :with => search_term
click_on 'search-btn'
end
thanks it helped but..another issue occurs:(
./features/step_definitions/steps.rb:10:in /^videos of large rodents are returned$/'
./features/youtube_search.feature:5:inThen videos of large rodents are returned'
DEPRECATION: Using should from rspec-expectations' old :should syntax without explicitly enabling
i changed line:
Then(/^videos of large rodents are returned$/) do
page.should have_content 'Largest rodents'
end
page.sould on page.expect but did not help
i changed also text "Largest rodents" foe another:( and not helped:(

Is there any issues to have `end` as a method name?

I am working on a RoR project, and I want to know if I can use end as a method name. It seems to work fine, but I would like to know if this method will bring any issues in the future. I tried and it works:
class Dany
def end
puts 'Hola'
end
end
and this is the output:
Dany.new.end # => Hola
Ruby let's you do this, but you're going to run into all sorts of issues.
# end.rb
class Dany
def end
puts "Hola"
end
def other
end # should puts Hola
end
end
Instead, you will get
end.rb:10: syntax error, unexpected keyword_end, expecting end-of-input
Bottom line: don't do this. Don't use any keywords as a method name.
It is not a good idea to use a keyword as a method name, but as long as you disambiguate the token as a method call, you can use it. It is not practical though.
Dany.new.instance_eval{self.end} # => Hola
Dany.new.send(:end) # => Hola
Dany.new.method(:end).call # => Hola
Dany.new.instance_eval{end} # => syntax error, unexpected keyword_end
The usual disambiguation using () does not seem to work for this case, making it complicated.
Dany.new.instance_eval{end()} # => syntax error, unexpected keyword_end

Syntax error in content type case statement

This is my code in my Padrino application and I can't figure out what line or bug it is. The error message is "syntax error, unexpected keyword_end expecting $end"
get :index, :provides => [:html, :json] do
#title = "Restaurants"
#restaurants = Restaurant.all
case content_type
when :json
render #restaurants
else
render 'restaurants/index'
end
end
end
Could you please point out my mistake and also suggest how I might debug it in future? Thanks
You have one spare end keyword.
You should remove one.
There is a little mess with indentation in your code. Keeping right indentation helps a lot in avoiding such errors. I would suggest to indent your code like this:
get :index, :provides => [:html, :json] do
#title = "Restaurants"
#restaurants = Restaurant.all
case content_type
when :json
render #restaurants
else
render 'restaurants/index'
end
end
There's an end too much.
Be more careful with your code indentation and this will never be a problem. Example on how it would look in Vim under. I just used =G and it aligned it for me. Additionaly, it'll only highlight the correct use of end. Your favorite editor of choice should have this functionality too. If not, switch.
try this:
get :index, :provides => [:html, :json] do
#title = "Restaurants"
#restaurants = Restaurant.all
case content_type
when :json
render #restaurants
else
render 'restaurants/index'
end
end

Using class_eval to overwrite an association

I have a Message model that has the following relationships:
belongs_to :sender, Class: "User"
belongs_to :recipient, Class: "User"
I'm attempting to use class_eval to overwrite the recipient method in certain cases.
This works:
def update_recipient(message, recipient_addition = nil)
message.class_eval <<-EVAL
def recipient
"test"
end
EVAL
end
message.recipient => "test"
However, this doesn't:
def update_recipient(message, recipient_addition = nil)
message.class_eval <<-EVAL
def recipient
[#{message.recipient}, #{recipient_addition}]
end
EVAL
end
(eval):3: syntax error, unexpected keyword_end, expecting ']'
The first # is misinterpreted as a comment character, discarding the rest of the line. The #{} are expected to be interpolated inside double quotes, though there doesn't seem to be a reason to put these in #{} right now as they are just simple string values.
["#{message.recipient}", "#{recipient_addition}"]
... unless you're planning something like:
["To: #{message.recipient}", "CC: #{recipient_addition}"]

NoMethodError with 'sign_in' using Devise::TestHelpers

I'm trying to get Devise's helper methods to work for a controller spec. Here's the relevant code:
# spec/spec_helper.rb
RSpec.configure do |config|
# other config stuff ...
config.include Devise::TestHelpers, :type => :controller
end
#spec/controllers/posts_controller_spec.rb
require 'spec_helper'
describe PostsController do
describe "GET index" do
admin = FactoryGirl.create(:admin)
sign_in admin # NoMethodError occurs
it "does something..." do
# etc etc etc
end
You have to be authenticated to render the template from this route, which is why I'm trying to sign in an admin so the test will pass. Thanks for your help.
I think that the sign_in method must be used in a context of it or before blocks. Try:
before do
admin = FactoryGirl.create(:admin)
sign_in admin # NoMethodError occurs
end

Resources