FactoryGirl deletes all users in database - ruby-on-rails

In Rails Tutorial 2nd Edition by Hartl
When running rspec tests the pagination block in user_pages_spec.rb is deleting all users in the development database instead of just the users created by FactoryGirl. Of course this causes other test to fail now that there aren't anymore users in the database.
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "index" do
let(:user) { FactoryGirl.create(:user) }
before(:each) do
sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
describe "pagination" do
before(:all) { 30.times { FactoryGirl.create(:user) } }
after(:all) { User.delete_all }
it { should have_selector('div.pagination') }
it "should list each user" do
User.paginate(page: 1).each do |user|
expect(page).to have_selector('li', text: user.name)
end
...
Gemfile
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'pg', '0.15.1'
gem 'activeresource', '4.0.0'
group :development, :test do
gem 'rspec-rails', '2.13.1'
gem 'annotate', '~> 2.5.0'
end
group :test do
gem 'selenium-webdriver', '2.0.0'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.0'
end
gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails', '2.2.1'
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'
end

You can try this.
30.times do
User.last.destroy
end
In place of
User.delete_all
This will delete all the 30 users created by FactoryGirl. But will work only if you are not creating more users in between.
Update
The above code was just a hack.
If while testing application is hitting development database, then config/database.yml file need to be checked to ensure that it's not pointing to same db for test and development.

You can also use something like Database Cleaner. Then you would not need to delete the users manually but all the changes to the database would be automatically reverted (when using the transaction strategy).

Related

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

Hartl Rails Tutorial

I ran into some trouble with section 6.2.1 (validity test) of Michael Hartl's "Rails Tutorial" and realized that I don't even have a test directory created as a result of
$ rails generate model User name:string email:string
While the tutorial says that I should see the output
invoke active_record
create db/migrate/20140724010738_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
all I am actually seeing is:
invoke active_record
identical db/migrate/20141020202519_create_users.rb
identical app/models/user.rb
invoke rspec
identical spec/models/user_spec.rb
I've researched tons of different sites looking for the answer and noticed people were suggesting to move rspec-rails around in the Gemfile but from what I can see, mine is placed properly. Here is my Gemfile:
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'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
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'
end
gem 'sass-rails', '4.0.3'
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 would really appreciate any help! Thanks
What is happening here is rspec-rails is overriding the default behavior of rails.
Rails by default ships with test_unit (as shown by the invoke test_unit in original output).
Instead with rspec, you generate spec files instead. Thus you have invoke rspec and spec/models/user_spec.rb.
If you remove rspec-rails from your Gemfile and run the bundle install and then rails g model User name:string email:string again, it will generate the test files according to the tutorial.
If you want the same output, put the related rspec gem in the test group only.
group :development, :test do
gem 'sqlite3', '1.3.8'
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 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
end
And the update:
bundle update
It worked for me for the inverse process (invoke rspec on generating the model) placing rspec gems into development and test groups.
If you don't want to use rspec as the test suite, just remove the gems from your gemfile.
It looks like the online version of the Rails Tutorial has recently undergone a major overhaul. Previously, the tutorial used rspec for testing; now it's using the test framework that comes with rails. If you look at section 3.1, the Gemfile for the sample app now looks like this:
source 'https://rubygems.org'
gem 'rails', '4.2.0.beta2'
gem 'sass-rails', '5.0.0.beta1'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '4.0.0.beta2'
gem 'turbolinks', '2.3.0'
gem 'jbuilder', '2.2.3'
gem 'rails-html-sanitizer', '1.0.1'
gem 'sdoc', '0.4.0', group: :doc
group :development, :test do
gem 'sqlite3', '1.3.9'
gem 'byebug', '3.4.0'
gem 'web-console', '2.0.0.beta3'
gem 'spring', '1.1.3'
end
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
end
And in section 3.3.1, the first test looks like this:
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
end
test "should get help" do
get :help
assert_response :success
end
end
Because the tutorial is now using a different test framework, you will no longer get the same output as the tutorial. And in fact, you will no longer be able to follow along with the tests in the tutorial. Unfortunately, I think you will have to start over. I was on chapter 7, so I am in the same position as you. I emailed the author to see if he would be willing to put up the old version somewhere.
Michael Hartl got back to me. Here's a link to the old version:
http://rails-4-0.railstutorial.org/book

uninitialized constant TestFactories (NameError)

I am a beginner in Ruby on Rails and I am writing a "User sign in" spec for a wiki project, and I am getting the following error:
uninitialized constant TestFactories (NameError)
This is my sign_in_spec.rb
require 'rails_helper'
describe "Sign in flow" do
include TestFactories
before do
#user = authenticated_user
end
describe "successful" do
it "redirects user to the wikis index" do
user = authenticated_user
visit root_path
end
end
end
This is my test_factories.rb file:
module TestFactories
def authenticated_user(options={})
user_options = { email: "email#{rand}#fake.com", password: 'password' }.merge(options)
user = User.new(user_options)
user.skip_confirmation!
user.save
user
end
end
This is my Gemfile:
source 'https://rubygems.org'
gem 'rails'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'bootstrap-sass', '~> 3.2.0'
gem 'autoprefixer-rails'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# Testing
group :develpment, :test do
gem 'rspec-rails'
gem 'capybara'
gem 'database_cleaner'
gem 'factory_girl_rails', '~> 4.0'
gem 'pry-rails'
end
# Databases
# Developemnt
gem 'sqlite3'
or you can do something like this in sepc helper
RSpec.configure do |config|
config.include TestFactories
end

Im getting error : No DRb server is running. Running in local process instead

I'm following the ROR tutorials , Im testing with rspec spec/requests/static_pages_spec.rb , Error occurred " No DRb server is running. Running in local process instead"' I did some research , they said it's because Spork server is not running , so i added Spork.each_run do to the spc file, it dosen't help , I also modified the gem file from gem 'guard-spork' to gem 'guard-spork', :github => 'guard/guard-spork' Still the same, Anyone can help? Thanks in Advance!
Spec file:
require 'spec_helper'
Spork.each_run do
end
describe "Static pages" do
let(:base_title) { "Ruby on Rails Tutorial Sample App" }
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the base title" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit '/static_pages/home'
expect(page).not_to have_title('| Home')
end
describe "Contact page" do
it "should have the content 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_content('Contact')
end
it "should have the title 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
end
end
end
end
Gemfile :
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.3.2.0'
gem 'pg', '0.15.1'
group :development, :test do
gem 'guard-spork', :github => 'guard/guard-spork'
gem 'sprockets', '2.11.0'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', '4.0.0'
gem 'childprocess', '0.3.6'
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', '2.2.1'
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 'rails_12factor', group: :production
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
You should run spork in different process with command spork. Also, remove Spork part from test and modify you spec_helper file with something like this (move all in Spork.prefork block)
The other thing is that Spork had been replaced (not directly) by spring that does the same thing, but without additional configuration.

Why does RSpec report a syntax error in this spec?

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.

Resources