I'm just getting started with Ruby on Rails and I already feel like an idiot being stuck on something that seems so simple.
I'm stuck on Chapter 7.3.4 of Michael Hartl's Ruby on Rails tutorial. I'm doing an Integration Test on a user signup to check for invalid form submission.
I've thoroughly followed the tutorial so far and have been able to grasp every concept or error I ran into, but this one got me stuck. Upon trying Rails t in the Console (Ubuntu), I'm getting the following error :
ERROR["test_invalid_signup_information", #<Minitest::Reporters::Suite:0x000055b0dec5f190 #name="UsersSignupTest">, 1.8036143100000004]
test_invalid_signup_information#UsersSignupTest (1.80s)
ArgumentError: ArgumentError: wrong number of arguments (given 2, expected 1)
test/integration/users_signup_test.rb:8:in `block (2 levels) in <class:UsersSignupTest>'
test/integration/users_signup_test.rb:7:in `block in <class:UsersSignupTest>'
19/19: [=================================] 100% Time: 00:00:01, Time: 00:00:01
Finished in 1.84116s
19 tests, 38 assertions, 0 failures, 1 errors, 0 skips
Here is the test file itself, where the error comes from :
require "test_helper"
class UsersSignupTest < ActionDispatch::IntegrationTest
test "invalid signup information" do
get signup_path
assert_no_difference 'User.count' do
post users_path, params: { user: { name: "",
email: "user#invalid",
password: "foo",
password_confirmation: "bar" } }
end
assert_template 'users/new'
end
end
Here is the User controller file :
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
# Handle a successful save.
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
And here is my Gemfile :
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.0.0'
gem 'rails', '6.1.0'
gem 'bcrypt', '3.1.13'
gem 'bootstrap-sass', '3.4.1'
gem 'puma', '5.0.4'
gem 'sass-rails', '6.0.0'
gem 'webpacker', '4.2.2'
gem 'turbolinks', '5.2.1'
gem 'jbuilder', '2.10.0'
gem 'rexml'
gem 'bootsnap', '1.4.6', require: false
group :development, :test do
gem 'sqlite3', '1.4.2'
gem 'byebug', '11.1.3', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
gem 'web-console', '4.1.0'
gem 'listen', '3.4.1'
gem 'spring', '2.1.1'
gem 'spring-watcher-listen', '2.0.1'
end
group :test do
gem 'capybara', '3.32.2'
gem 'selenium-webdriver', '3.142.7'
gem 'webdrivers', '4.3.0'
gem 'rails-controller-testing', '1.0.4'
gem 'minitest', '5.11.3'
gem 'minitest-reporters', '1.3.8'
gem 'guard', '2.16.2'
gem 'guard-minitest', '2.4.6'
gem 'pry'
end
group :production do
gem 'pg', '1.2.3'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
# Uncomment the following line if you're running Rails
# on a native Windows system:
# gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
I've searched for similar threads and actually found this on StackOverflow :
Chapter 7 Errors, Ruby on Rails tutorial
I do think it's the syntax of the test that must be changed, and I've tried the solution suggested on there, but the test still fails tells me about a wrong number of arguments. It must be pretty simple, although I'm just not seeing it...
Thank you guys in advance for your time and help !
I was receiving and was baffled by the same error. It says that on the line with the TestCase method post I was passing two arguments, while it expected just one. I'm not sure why, since the documentation for that method indicates that it does indeed take two arguments - an action and an args hash.
Anyway, I decided to just follow the logic of the error and write it so that it's only one argument being passed to post - I removed the comma between users_path and params:
assert_no_difference 'User.count' do
post users_path params: { user: { name: "", email: "user#invalid", password: "foo", password_confirmation: "bar" } }
end
I have no idea why that works, especially as it seems to be wrong according to the documentation. But the test now passes without errors.
Related
maybe is a dumb question, but I am having an issue when I am trying to run a test on Ruby, this is the error:
Error: UsersLoginTest#test_login_with_valid_information:
NoMethodError: undefined method `User' for #UsersLoginTest:0x0000555ef3ec15b0 Did you mean? super
test/integration/users_login_test.rb:6:in `setup'
I tried to fix the fixture .yml but I am not sure that the problem is there anymore.
This is my users.yml
michael:
name: Michael Example
email: michael#example.com
password_digest: <%= User.digest('password') %>
this is my users_login_test.rb
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
#user = users(:michael)
end
test "login with valid information" do
get login_path
post login_path, params: { session: { email: #user.email,
password: 'password' } }
assert_redirected_to #user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(#user)
end
end
and this is my test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
class UserTest < ActiveSupport::TestCase
fixtures :all
include ApplicationHelper
setup do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
test "should be valid" do
assert #user.valid?
end
end
where could be the problem? thank you very much for your help
New Attachments
irb(main):002:0> User.column_names
=> ["id", "name", "email", "created_at", "updated_at", "password_digest"]
I tried changing password_digest: 123456, but it throws the same error.
I tried changing to password: test_password, and this comes up:
UserTest#test_should_be_valid:
ActiveRecord::Fixture::FixtureError: table "users" has no columns
named "password".
Also I am attaching my gem file, where the only file I intentional used to authentication was gem 'bcrypt' I think:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.3'
gem 'rails', '6.0.1'
gem 'autoprefixer-rails', '9.6.1.1'
gem 'uglifier', '3.2.0'
gem 'coffee-rails', '5.0.0'
gem 'jquery-rails', '4.3.5'
gem 'mini_magick', '4.9.5'
gem 'will_paginate', '3.2.1'
gem 'bootstrap-will_paginate', '1.0.0'
gem 'bootstrap-sass', '3.4.1'
gem 'puma', '4.3.1'
gem 'font-awesome-rails', '4.7.0.5'
gem 'sass-rails', '6'
gem 'webpacker', '4.0'
gem 'turbolinks', '5'
gem 'jbuilder', '2.9.1'
gem 'rubocop', '0.77.0'
gem 'bootsnap', '1.4.2', require: false
gem 'rails-controller-testing'
gem 'bcrypt', '3.1.12'
group :development, :test do
gem 'sqlite3', '1.4.1'
gem 'byebug', platforms: %i[mri mingw x64_mingw]
end
group :development do
gem 'web-console', '3.3.0'
gem 'listen', '3.2.0'
gem 'spring'
gem 'spring-watcher-listen', '2.0.0'
end
group :test do
gem 'capybara', '3.28.0'
gem 'selenium-webdriver', '3.142.4'
gem 'webdrivers', '4.1.2'
end
group :production do
gem 'pg', '0.20.0'
# gem 'fog', '1.42'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
And just in case I created a repository: https://github.com/cochabambinoski/Share-Exercise
Ok, your problem is you set up test_helper.rb as a test itself and that is causing you problems. Your file should look like:
#test_helper.rb
ENV['RAILS_ENV'] = 'test'
require_relative '../config/environment'
require 'rails/test_help'
class ActiveSupport::TestCase #you were creating a new class in your code
fixtures :all
include ApplicationHelper
end
That's it, all I had to do after cloning your repo was to bundle install, and do a rake db:create and rake db:test:prepare. Now the test passes. Don't forget to change your fixture back to:
michael:
name: Michael Example
email: michael#example.com
password_digest: <%= User.digest('password') %>
Big lesson here is test_helper.rb is a place for some configuration and settings, not the place to put tests.
I've got a weird situation and I'm not sure how to even start debugging it. Would love any advice or direction that someone can provide.
I've got 64 rspec tests - view, model, controller, feature - and they all pass just fine. As soon as I simply add gem 'wicked_pdf' to the Gemfile, do bundle install, and then re-run the tests, all 16 of the view specs fail, but all other tests run fine. Not only that, the entire app runs fine, local and on Heroku. So it's only rspec that is having the issue; perhaps something in wicked_pdf is conflicting with rspec...?
I don't think the issue is in my code per se, but I'll add whatever I can think of on the off chance it contains a clue. One of the 16 now-failing view specs is:
describe "properties/show.html.erb" do
it "displays a field's name and value" do
assign(:property,
FactoryGirl.build_stubbed(:property, stories: 2))
render
expect(rendered).to match(/Stories.*2/m)
end
... rest ommitted
One of the error messages is (they are all the same):
1) properties/show.html.erb displays a field's name and value
Failure/Error: <%= ps_markup_field_or_hide("Year Built", #property.yearbuilt, false,
ActionView::Template::Error:
undefined method `ps_markup_field_or_hide' for #<#<Class:0x007f2b1123e0e0>:0x0000000968d978>
# ./app/views/properties/_show_body_post.html.erb:6:in `_app_views_properties__show_body_post_html_erb__2822263843794330255_69911472833980'
# ./app/views/properties/show.html.erb:19:in `_app_views_properties_show_html_erb__3990796008770089996_79086380'
# ./spec/views/properties/show.html.erb_spec.rb:19:in `block (2 levels) in <top (required)>'
# /usr/local/rvm/gems/ruby-2.3.0/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
# -e:1:in `<main>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `ps_markup_field_or_hide' for #<#<Class:0x007f2b1123e0e0>:0x0000000968d978>
# ./app/views/properties/_show_body_post.html.erb:6:in `_app_views_properties__show_body_post_html_erb__2822263843794330255_69911472833980'
The view that contains the red-herring "undefined method" contains many calls like this to the ps_markup_field_or_hide helper function:
<%= ps_markup_field_or_hide("Year Built", #property.yearbuilt, false,
#property.yearbuiltflag, "A",
" (actual)", " (estimated)") %>
That function is defined in app/helpers/views/properties/show_helper.rb:
module Views::Properties::ShowHelper
def ps_markup_field_or_hide(name, value, hide=false,
ext_field=nil, ext_conf_val=nil,
ext_true_str=nil, ext_false_str=nil
)
title = "<div class='hanging-indent'> " +
"<span class='ps-details-titles'> #{name}: </span>"
conf_str = ""
if (ext_conf_val != nil)
conf_str = ext_false_str
if (ext_field != nil && ext_field.upcase == ext_conf_val.upcase)
conf_str = ext_true_str
end
end
if (value != nil)
(" " + title + value + conf_str + "\n </div>").html_safe
else
(" " + title + "Not on File\n </div>").html_safe unless hide
end
end
end #module Views::Properties::ShowHelper
I started hacking, trying to kludge a workaround, so I blindly (I'm a ruby beginner) added an include Views::Properties::ShowHelper statement to the controller; then I tried hard-coding the fully qualified path to the method in the view: Views::Properties::ShowHelper.ps_markup_field_or_hide. I even assigned the ps_markup_field_or_hide() method to a view variable in the controller and tried to call the function from within the view that way. Nothing worked - same error each time - and my attempts were getting so kludgey that even I became disgusted and backed away.
My Gemfile:
source 'https://rubygems.org'
gem 'rails', '5.0.0.1'
gem 'responders', '2.3.0'
gem 'puma', '3.4.0'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.0.0'
gem 'coffee-rails', '4.2.1'
gem 'jquery-rails', '4.1.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.4.1'
gem 'redcarpet', '~> 3.0.0'
gem 'normalize-scss', '~> 4.0', '>= 4.0.3'
gem 'activemodel-serializers-xml'
gem 'draper', github: 'drapergem/draper'
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
gem 'fuzzy-string-match'
group :development, :test do
gem 'byebug', '9.0.0', platform: :mri
gem 'spring-commands-rspec'
gem 'rspec-rails', '~> 3.5'
gem 'guard-rspec'
gem 'capybara', '~> 2.5'
gem 'selenium-webdriver'
gem 'factory_girl_rails', '~> 4.5.0'
gem 'database_cleaner', '1.3.0'
end
group :development, :test, :production do
gem 'mysql2', '>= 0.3.18', '< 0.5'
end
group :development do
gem 'web-console', '3.1.1'
gem 'listen', '3.0.8'
gem 'spring', '1.7.2'
gem 'spring-watcher-listen', '2.0.0'
gem 'pry-byebug'
end
group :test do
gem 'sqlite3'
gem 'rails-controller-testing', '0.1.1'
gem 'minitest-reporters', '1.1.9'
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
end
group :production do
# gem 'pg', '0.18.4'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
I'm developing using the Cloud9 IDE.
What else might be helpful... here's the config/initializers/wicked_pdf.rb:
# WickedPDF Global Configuration
# https://github.com/mileszs/wicked_pdf/blob/master/README.md
if Rails.env.production?
wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-binary-0.12.3.1/bin/wkhtmltopdf"
WickedPdf.config = { exe_path: wkhtmltopdf_path, wkhtmltopdf: wkhtmltopdf_path }
else
WickedPdf.config = {
}
end
I was looking at https://github.com/rspec/rspec-rails/issues/1644, where adding the rails-controller-testing gem caused rspec to fail with a NoMethodError, but I couldn't see how to apply any of the suggestions on that post to this situation. I tried modifying the Gemfile adding the "require: false" (gem 'wicked_pdf', require: false), re-running bundle install, and placing the require 'wicked_pdf' in several other places, but that only caused other errors and I felt like I was poking around in the dark and not in the right direction.
All in all, I'm stumped as to how to proceed, and if anyone has any ideas, or could suggest the next debugging step, I'd truly appreciate it.
Thank you so much -- Derek.
PS. I run my tests on cloud9 using: xvfb-run bundle exec spring rspec spec --format doc, and I am using an external mysql database. I also have the ability to run my tests locally on sqlite, which I usually do for speed because the external service is so slow, but I've tried the tests both using mysql and sqlite and I get the same error messages either way.
PPS. My current workaround is I comment out the wicked_pdf gem and the wicked_pdf.rb file, re-run bundle install and run all the tests, then when I get around to writing the PDF specs, I'll "turn back on" the wicked_pdf stuff and just run the PDF specs separately. But that's such a mess, and I'd like to have the tests all running clean in one fell swoop so I can get a CI workflow going (on the todo list).
$ bundle show rails
/usr/local/rvm/gems/ruby-2.3.0/gems/rails-5.0.0.1
$ bundle show wicked_pdf
/usr/local/rvm/gems/ruby-2.3.0/gems/wicked_pdf-1.1.0
$ bundle show rspec-rails
/usr/local/rvm/gems/ruby-2.3.0/gems/rspec-rails-3.5.2
I have run through mhartls's rails tutorial a few times with rails 4.x and just for fun, I started again, but updating to rails 5.0.0.beta1.
I have managed to find solution to all of the incompatibilities up to chapter 9. Specifically, I can't get the test of the destroy action on the users controller to work. I get:
ERROR["test_should_redirect_destroy_when_not_logged_in", UsersControllerTest, 1.489256506320089]
test_should_redirect_destroy_when_not_logged_in#UsersControllerTest (1.49s)
URI::InvalidURIError: URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80destroy
test/controllers/users_controller_test.rb:49:in `block (2 levels) in <class:UsersControllerTest>'
test/controllers/users_controller_test.rb:48:in `block in <class:UsersControllerTest>'
URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80destroy
test/controllers/users_controller_test.rb:49:in `block (2 levels) in <class:UsersControllerTest>'
test/controllers/users_controller_test.rb:48:in `block in <class:UsersControllerTest>'
bin/rails test test/controllers/users_controller_test.rb:47
my controller destroy action:
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end
My test :
test "should redirect destroy when logged in as a non-admin" do
log_in_as(#other_user, integration_test: 'true')
assert_no_difference 'User.count' do
delete :destroy, params: { id: #user }
end
assert_redirected_to root_url
end
The links actually work, but test fails. I can get the test to pass by adding a route like this: delete 'delete_user' => 'users#destroy' AND changing the test to: delete delete_user_path params:{id: #user} however, the actual site and the test are using different routes, so I don't know if I can trust this. If I remove destroy from the users resources: resources :users, except: :destroy then the page link doesn't work but the test still passes. I don't see how to use the delete_user route on the page link and cant seem to get the user#destroy route to work in the test. I am aware that this is not an integration test, but this workaround fixed several other problems. I will refactor later, so that it makes sense.
Additional related controller code:
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user.
def correct_user
#user = User.find(params[:id])
redirect_to(root_url) unless current_user?(#user)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
Test Helper Code:
# Logs in a test user. (I think all tests must be like integration)
def log_in_as(user, options = {})
password = options[:password] || 'password'
remember_me = options[:remember_me] || '1'
# the next line is the work around for rails version >=5.1
integration_test = options[:integration_test] || 'false'
if integration_test == 'true'
post login_path, params: { session: { email: user.email,
password: password,
remember_me: remember_me }
}
else
# params[:session][:user_id] = user.id
# session[:user_id] = user.id
controller.session[:user_id] = user.id
# params[session[:user_id] ] = user.id
end
end
Gemfile:
source 'https://rubygems.org'
ruby '2.3.0'
gem 'rails', '>= 5.0.0.beta1'
gem 'railties', '>= 5.0.0.beta1'
gem 'bcrypt', '>= 3.1.10'
gem 'faker', '>= 1.6.1'
gem 'carrierwave', '>= 0.10.0'
gem 'mini_magick', '>= 4.3.6'
gem 'fog', '>= 2.0.0.pre.0'
gem 'kaminari', :git => "git://github.com/amatsuda/kaminari.git", :branch => 'master'
gem 'kaminari-bootstrap', '>= 3.0.1'
gem 'bootstrap-sass', '>= 3.3.6'
gem 'sass-rails', '>= 5.0.4'
gem 'uglifier', '>= 2.7.2'
gem 'coffee-rails', '>= 4.1.1'
gem 'jquery-rails', '>= 4.0.5'
gem 'therubyracer', '>= 0.12.2'
gem 'turbolinks', github: 'rails/turbolinks'
gem 'jbuilder', '>= 2.4'
gem 'sdoc', '>= 0.4.1', group: :doc
gem 'net-ssh'
group :development, :test do
gem 'sqlite3', '>= 1.3.11'
gem 'byebug', '>= 8.2.1'
gem 'spring', '>= 1.6.1'
end
group :development do
gem 'web-console', '>= 3.0.0'
end
group :test do
gem 'minitest-reporters', '>= 1.1.7'
gem 'mini_backtrace', '>= 0.1.3'
gem 'guard', '>= 2.13.0'
gem 'guard-minitest', '>= 2.4.4'
gem 'minitest', '>= 5.8.3'
gem 'rails-controller-testing', '>= 0.0.3'
end
group :production do
gem 'pg', '>= 0.18.4'
gem 'rails_12factor', '>= 0.0.3'
gem 'puma', '>= 2.15.3'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', '>= 1.2015.7', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
The correct solution is to replace:
delete :destroy, params: { id: #user }
with:
delete user_url(#user)
It seems that my in current setup ( see gemfile etc.) tokens ( :destroy , :user, etc) are not handled the same as in my previous setup (like mhartl recommends) so the fix I use is to replace all of the tokens in my tests with named routes (:user becomes user_path(#user). The destroy action doesn't seem to have a default named route so I added one to the routes.rb file.
I can get the test to pass by adding a route like this:
delete 'delete_user' => 'users#destroy'
AND changing the test to:
delete delete_user_path params:{id: #user}
however, the actual site and the test are using different routes to the same action, so I don't know if I can trust this as a final solution.
If i remove destroy from the users resources:
resources :users, except: :destroy
then the page link doesn't work but the test still passes. I don't see how to use the added 'delete_user(#user) route' on the page link, I keep getting user not found when I try, and I cant seem to get the user#destroy action to work in the test without the added route.
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.
when I toggle :js => true on my examples, takes too long very long to come up and run the first test, then becomes acceptable peformance
this sample running in 0.36722 seconds without :js, and 58.15 seconds with :js => true
require 'spec_helper'
include UserAbilitiesHelper
describe "Customer Task Pages" do
subject { page }
describe "side panel with the history of the tasks related to customer" do
before do
visit root_path()
end
it { sould have_content(I18n.t("customers.tasks.side.title")) }
end
describe "side panel with the history of the tasks related to customer" do
before do
visit root_path()
end
it { sould have_content(I18n.t("customers.tasks.side.title")) }
end
end
my Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.5'
gem 'pg', '0.12.2'
gem "meta_search"
# Bootstrap and layouting
gem 'bootstrap-sass', '2.0.3'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
gem 'rails3-jquery-autocomplete'
#test support
gem 'faker', '1.0.1'
#login rules
gem 'devise'
gem 'cancan'
#criptar
gem 'bcrypt-ruby', '3.0.1'
#BR
gem 'brazilian-rails'
gem 'rails-i18n'
group :development do
gem 'annotate', '~> 2.4.1.beta'
gem 'nested_scaffold'
end
group :development, :test do
gem 'rspec-rails', '2.10.0'
gem 'guard-rspec', '0.5.5'
gem 'guard-spork', '0.3.2'
gem 'spork', '0.9.0'
gem 'ruby-debug19'
gem 'linecache19'
gem 'factory_girl_rails', '1.4.0'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test do
gem 'capybara', '1.1.2'
gem "launchy"
gem 'ZenTest'
#MAC OS Especific
gem 'rb-fsevent', '0.4.3.1', :require => false
gem 'growl', '1.0.3'
#Cucumber
gem 'cucumber-rails', '1.2.1', require: false
gem 'database_cleaner', '0.7.0'
end
group :production do
#gem 'pg', '0.12.2'
end
running with
bundle exec rspec
An example any
Bruno-Guerras-MacBook-Pro:railstutorial brunoguerra$ bundle exec rspec spec/requests/authentication_pages_spec.rb
No DRb server is running. Running in local process instead ...
WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.7.3
.....................
Finished in 6.07 seconds
21 examples, 0 failures
Bruno-Guerras-MacBook-Pro:railstutorial brunoguerra$ bundle exec rspec spec/requests/authentication_pages_spec.rb
No DRb server is running. Running in local process instead ...
WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.7.3
............F........
Failures:
1) Authentication authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page
Failure/Error: page.should have_selector('title', text: 'Edit user')
expected css "title" with text "Edit user" to return something
# ./spec/requests/authentication_pages_spec.rb:65:in `block (6 levels) in <top (required)>'
Finished in 1 minute 14.18 seconds
21 examples, 1 failure
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:64 # Authentication authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page
Bruno-Guerras-MacBook-Pro:railstutorial brunoguerra$
thanks
I discovery, my dns server is too slow, I changed my dns server and the problem solved, another thing I did to improve the speed of testing was to configure these parameters webrick
configure webrik speedup but, its solve the same problem, DNS server! urgh!!!
You can also try changing the driver :webkit_debug and then rerun your specs to see any if there are any scripts that don't need to be loaded on the page.
Capybara.javascript_driver = :webkit_debug
Then add any urls to the black list
config.before(:each, js: true) do
page.driver.browser.url_blacklist = ["http://use.typekit.net"]
end