Why does RSpec report a syntax error in this spec? - ruby-on-rails

When trying to run rspec I keep getting the error message below when running bundle exec rspec spec/models/user_spec.rb command. I have searched similar issues and have seen suggestions for entering in exact paths and making sure I was in the root directory. Neither have worked. Additionally, I have tried running a bundle install. I apologize if this is a rudimentary question. I'm a total noob. Thanks in advance for the help.
/Users/sethjones/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load': /Users/sethjones/rails_projects/sample_app/spec/models/user_spec.rb:6: syntax error, unexpected '}', expecting => (SyntaxError)
/Users/sethjones/rails_projects/sample_app/spec/models/user_spec.rb:20: syntax error, unexpected end-of-input, expecting keyword_end
from /Users/sethjones/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /Users/sethjones/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /Users/sethjones/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /Users/sethjones/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/sethjones/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /Users/sethjones/.rvm/gems/ruby-2.0.0-p481/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
My gemfile is as follows:
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.5'
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'
gem 'bcrypt-ruby', '3.1.2'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
# The following optional lines are part of the advanced setup. # gem ’guard-rspec’, ’2.5.0’
# gem ’spork-rails’, ’4.0.0’
# gem ’guard-spork’, ’1.5.0’
# gem ’childprocess’, ’0.3.6’
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.0'
gem 'cucumber-rails', '1.4.0', :require => false
gem 'database_cleaner', github: 'bmabey/database_cleaner'
gem 'growl', '1.0.3'
# Uncomment these lines on Linux.
# gem ’libnotify’, ’0.8.0'gem i
# Uncomment these lines on Windows.
# gem ’rb-notifu’, ’0.0.4’
# gem ’wdm’, ’0.1.0’
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
And my user_spec.rb file looks like this:
require 'spec_helper'
describe User do
before do
{ #user = User.new(name: "Example User", email: "user#example.com") }
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should be_valid }
describe "when name is not present" do
before { #user.name = " "}
it { should_not be_valid }
end
end
Thanks again!

The block you're passing to before is surrounded by both do/end and curly braces. Since the curlies don't follow a method invocation, Ruby interprets them as a hash, and expects a => before the closing curly. Just remove the curlies.

Related

i am unable to execute the sample_app gem file

There was an error parsing Gemfile:
syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' - ...m 'database_cleaner', git clone 'git://github.com/maccman/gd...
... ^
/home/phorekka/rails_project/sample_app/Gemfile:28: syntax error, unexpected unary-, expecting keyword_do or '{' or '('
...an/gdata.git' "foobar" --bare --no-hardlinks
... ^. Bundler cannot continue.
# from /home/phorekka/rails_project/sample_app/Gemfile:28
# -------------------------------------------
# gem 'cucumber-rails', '1.3.0', :require => false >
gem 'database_cleaner', git clone 'git://github.com/maccman/gdata.git' "foobar" --bare --no-hardlinks
> Here is my gem file.
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.8'
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'
gem 'bcrypt-ruby', '3.1.2'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
# The following optional lines are part of the advanced setup.
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', '4.0.0'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.6'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.0'
gem 'cucumber-rails', '1.3.0', :require => false
gem 'database_cleaner', github: 'bmabey/database_cleaner'
# Uncomment this line on OS X.
#gem 'growl', '1.0.3'
# Uncomment these lines on Linux.
gem 'libnotify', '0.8.0'
# Uncomment these lines on Windows.
# gem 'rb-notifu', '0.0.4'
# gem 'win32console', '1.3.2'
# gem 'wdm', '0.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
I am using sqlite3 --version
3.8.11.1 2015-07-29 20:00:57 cf538e2783e468bbc25e7cb2a9ee64d3e0e80b2f.
I am not able to run this gemfile. Help me please.
Your Gemfile is different than the one in third chapter,
I just replaced your Gemfile with the one in book:
And then executed bundle install --without production and it works fine.
Also run bundle update after that and you'll be good to go.
Try it yourself and let me know if you have other question.

how to get open-uri works in a rails project (asked by a newbie)

my spec :
require 'spec_helper'
RSpec.describe Category, type: :model do
before { #category = FactoryGirl.build(:category) }
subject { #category }
...
my factory :
FactoryGirl.define do
factory :category do
title { FFaker::Lorem.word }
picture { FFaker::Avatar.image }
end
end
my gem file :
source 'https://rubygems.org'
gem 'rails', '4.2.4'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'sdoc', '~> 0.4.0', group: :doc
gem "devise"
gem 'font-awesome-rails'
gem 'simple_form'
gem 'sabisu_rails', github: "IcaliaLabs/sabisu-rails"
gem 'furatto', github: "IcaliaLabs/furatto-rails"
gem 'active_model_serializers', github: 'rails-api/active_model_serializers', branch: '0-8-stable'
gem "paperclip", "~> 4.3"
gem 'open_uri_redirections'
group :development, :test do
gem 'byebug'
gem 'factory_girl_rails'
gem 'ffaker'
end
group :development do
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'sqlite3'
group :test do
gem 'rspec-rails'
gem 'shoulda-matchers', '2.5.0', require: false
gem 'rspec-collection_matchers'
end
end
what I got running this command =>
bundle exec rspec spec/models/category_spec.rb is :
Randomized with seed 44232 FFFFF Failures:
1) Category
Failure/Error: before { #category = FactoryGirl.build(:category) }
RuntimeError: redirection forbidden: robohash.org/debitismagnamodio.png?size=300x300 -> robohash.org/debitismagnamodio.png?size=300x300
the factory can't create the object because it calls FFake::Avatar.image which is an url (I think, I'm not sure). I already required open-uri and open_uri_redirections both in the factory and the spec but it doesn't work.
any suggestions please ?
Try this in to you Gemfile
gem 'open_uri_redirections', git: 'git#github.com:open-uri-redirections/open_uri_redirections.git'
after
bundle update open_uri_redirections

Running rails-0.9.5 error

I was getting an error with my application, so I looked on stack and found an answer undefined method `require_relative' for main:Object (NoMethodError) it said that I should run gem install rails which I did, after I ran that, when I tried to run rails generate controller StaticPages home help --no-test-framework
I got an error:
(in /usr/local/rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rails-0.9.5)
rake aborted!
ERROR: 'rake/rdoctask' is obsolete and no longer supported. Use 'rdoc/task' (available in RDoc 2.4.2+) instead.
/usr/local/rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rails-0.9.5/Rakefile:3:in `<top (required)>'
It says that I am running rails .95 and even when I ran gem update rails nothing happend
Here is my gemfile
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.2'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
Try deleting your Gemfile.lock (or perhaps moving it). This worked for me.

Error running rspec test in rails

Hi I have created a test case in spec/myprocess as follows
before :each do
#my_base = MyBase.new
end
describe "new_my_base" do
it "should not take any arguments and retuens a MyBase object" do
#my_base.should be_an_instance_of MyBase
end
end
describe "#my_path" do
it "returns the correct mybase file path" do
$path.should eql App::Application.config.my_encode_type.to_s
end
end
and when I tun rspec spec/lib/mybasetest.rb it throws me the following error.
kalanamith#kalanamith:~/Documents/projects/leena$ rspec spec/lib/mybasetest.rb
*** Mocha deprecation warning: Change `require 'mocha'` to `require 'mocha/setup'`.
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
*** Mocha deprecation warning: `require 'mocha/standalone'` has been deprecated. Please use `require 'mocha/api' instead.
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- mocha/object (LoadError)
/home/kalanamith/Documents/projects/fakturabank/spec/lib/mybasetest.rb:4:in `<top (required)>': undefined method `before' for main:Object (NoMethodError)
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `block in load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `map'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'
This is my gem file
source 'https://rubygems.org'
gem 'rails', '3.2.13'
gem 'jquery-rails' , '3.0.0'
gem 'pg'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
group :development, :test do
gem 'rails-dev-tweaks', '~> 0.6.1'
gem 'debugger'
gem 'rspec-rails', '2.10.0'
gem 'annotate'
gem 'factory_girl_rails'
gem 'shoulda-matchers'
gem 'simplecov', :require => false
gem 'simplecov-rcov', :require => false
gem 'pry'
gem "guard-rspec"
end
group :test do
gem 'mocha'
end
group :test do
gem "webmock"
end
gem 'debugger' # This gem works with ruby 1.9.3 it wraps these other things
gem 'savon', "~> 1.2.0"
gem 'airbrake'
gem 'aasm'
gem 'paranoia'
gem 'acts_as_tree'
gem 'will_paginate'
gem 'translate-rails3'
gem 'wicked_pdf'
gem 'activemerchant'
gem 'libxml-ruby'
gem 'libxslt-ruby'
gem 'ruby-xslt'
gem 'exception_notification', :require => 'exception_notifier'
gem "uuidtools", "~> 2.1.3"
gem "ruby-filemagic"
gem "rmagick", :require => false
gem "rubyzip"
gem "spreadsheet"
gem "multi_json"
gem "multi_xml"
gem "httparty"
#FormBuilder backwards compatibility
gem "dynamic_form"
gem "prototype-rails"
gem "formtastic"
gem "net-ssh"
gem "net-scp"
Um quite new to rails and this is my first experience with rspec framework . I will be grateful if anyone can assist me. Thank you in advance
There must be a "describe" that wraps before :each do
describe "Bla bla" do
before :each do
#my_base = MyBase.new
end
etc...
end
Anyway, the file name should be suffixed with _spec.rb
The before block should be inside the describe method
If you're using RSpec the end of the file must be suffixed with _spec.rb
Example: my_base_spec.rb
To Run: bundle exec rspec spec/my_base_spec.rb or simply bundle exec rspec

Clearance stops rails server from running

I installed clearance and followed the last steps which were "rails generate clearance:install". However rails server does not start up for me when I try to open in terminal.
/Users/lexi87/areyoutaken/config/initializers/clearance.rb:4:in `block in <top (required)>': uninitialized constant Clearance::PasswordStrategies::BCrypt (NameError)
from /Users/lexi87/.rvm/gems/ruby-1.9.3-p374/gems/clearance-0.16.3/lib/clearance/configuration.rb:36:in `configure'
from /Users/lexi87/areyoutaken/config/initializers/clearance.rb:1:in `<top (required)>'
from /Users/lexi87/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.12/lib/rails/engine.rb:588:in `block (2 levels) in <class:Engine>'
Here's my Gemfile and I have bcrypt gem inside.
source 'https://rubygems.org'
gem 'rails', '3.2.12'
gem 'bootstrap-sass', '2.1'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.0.1'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
gem 'jquery-rails', '2.0.2'
gem 'annotate', '2.5.0'
gem 'clearance'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
gem 'guard-rspec', '1.2.1'
gem 'guard-spork', '1.4.2'
gem 'spork', '0.9.2'
gem 'rb-fsevent', '~> 0.9.1'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.5'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails', '4.1.0'
gem 'cucumber-rails', '1.2.1', :require => false
gem 'database_cleaner', '0.7.0'
# gem 'launchy', '2.1.0'
# gem 'rb-fsevent', '0.9.1', :require => false
gem 'growl', '1.0.3'
end
group :production do
gem 'pg', '0.12.2'
end
Any help would be greatly appreciated.
You appear to be running gem version 0.16.3 of clearance, which doesn't support the Clearance::PasswordStrategies::BCrypt password strategy. If you upgrade to v1.0.0.rc1 or higher than that should work.
The only supported password strategies in 0.16.3 are Clearance::PasswordStrategies::Blowfish and Clearance::PasswordStrategies::SHA1.
If you want to try a version that supports BCrypt, you can update your Gemfile to say:
gem 'clearance', :git => "git://github.com/thoughtbot/clearance.git", :tag => "v1.0.0.rc4"
That will pull a specific tag of that repository. You can leave off the :tag to grab the latest version from GitHub. Keep in mind, that it may not be as stable as 0.16.3.

Resources