Skip to content

Instantly share code, notes, and snippets.

@anoobbava
Last active October 26, 2017 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anoobbava/80d3492862455f14cca71735214620b4 to your computer and use it in GitHub Desktop.
Save anoobbava/80d3492862455f14cca71735214620b4 to your computer and use it in GitHub Desktop.
RSpec
1. gem install rspec
group :development, :test do
gem 'byebug', '~> 9.1'
gem 'factory_girl_rails', '~> 4.9'
gem 'launchy', '~> 2.4', '>= 2.4.3'
gem 'pry-rails', '~> 0.3.6'
gem 'rspec-rails', '~> 3.0'
gem 'spring', '~> 2.0', '>= 2.0.2'
gem 'web-console', '~> 3.5', '>= 3.5.1'
end
group :test do
gem 'capybara', '~> 2.15', '>= 2.15.4'
gem 'database_cleaner', '~> 1.6', '>= 1.6.1'
gem 'faker', '~> 1.8', '>= 1.8.4'
gem 'rspec', '~> 3.7'
gem 'shoulda-matchers', '~> 3.1', '>= 3.1.2'
end
1.1 gem 'rspec-rails' in the dev group so it will create spec once we create the controller via rails g model
2. rspec --init..
3.rails generate rspec:model user
4. rails generate rspec:controller User
5. copy the below to spec_helper:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
6.create test db:
.7.rake db:test:prepare
8.databasecleaner is a tool used to clean db when u need to clear the db:
in spec_helper:
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation, { :only => %w[users] }
in array give name u need.
then DatabaseCleaner.clean whenever you need to clean user
the common point is at after RSpec.config or at before(:all) do last
in psql manually create test_db and do rake db:migrate RAILS_ENV=test
8. gem install factory girl and do the below line to config page in spec_helper
config.before(:all) do
FactoryGirl.reload
end
9. to clear table data in test db:
spec_helper:
config.after :all do
ActiveRecord::Base.subclasses.each(&:delete_all)
end
10.to view test db: rails c test
if below error
undefined method `sign_in' for #<RSpec::Core:
then add below line to spec_helper:
config.include Devise::TestHelpers, type: :controller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment