Skip to content

Instantly share code, notes, and snippets.

@JayK31
Forked from catm705/gist:10011321
Created April 25, 2014 16:00
Show Gist options
  • Save JayK31/11294487 to your computer and use it in GitHub Desktop.
Save JayK31/11294487 to your computer and use it in GitHub Desktop.
----------------------------------------------------------------------
Steps to create Rails App
----------------------------------------------------------------------
1.) rails new app_name -d postgresql
'Jesse's way': rails g model Complaint descriptor:string address:string latitude:decimal longitude:decimal zip:integer
2.) database.yml → 'Phil's gist'
- put in: echo $USER in the terminal right after to make sure it works.
3.) rake db:create
4.) Insert ‘gem’ gist for development:
group :development, :test do
gem 'launchy'
gem 'capybara'
gem 'pry-rails'
gem 'rspec-rails', '~> 3.0.0.beta'
gem 'jasmine'
# gem 'guard'
# gem 'guard-rspec'
# gem 'guard-bundler', require: false
# gem 'terminal-notifier-guard'
gem 'shoulda-matchers'
gem 'factory_girl_rails'
gem 'rubocop'
gem 'annotate'
gem 'nyan-cat-formatter'
end
5. bundle install
------------------------------------------------------------------------
#### If you want 'GUARD' #######
----------------------------------------------------------------------
** bundle exec guard init → #will look to see what guard files you have
** bundle exec guard - now if you save anything in your files it’ll run.
- rspec fails if you’re in a different environment - so you have to run a test migration file??
** Line 4: (Guardfile) !!!
guard :rspec, cmd: 'bundle exec rspec' do
----------------------------------------------------------------------
RSPEC SET-UP
----------------------------------------------------------------------
1.) bundle exec rails g rspec:install
- set up spec files, test, model
"bundle exec rspec" --> Runs rspec only.
2.) *mkdir spec/models
*touch spec/models/user_spec.rb
require 'spec_helper'
describe User do
it { should validate_presence_of :name }
end
*touch app/models/user.rb
class User < ActiveRecord::Base
validates :name, presence: true
end
------------------------------------------------------------------------
MIGRATIONS, DATA MODELS:
----------------------------------------------------------------------
rails g migration create_users
t.string :name
t.string :email
t.string :phone_number
t.timestamps
rake db:migrate RAILS_ENV=test
----------------------------------------------------------------------
CAPYBARA ACCEPTANCE TESTS
----------------------------------------------------------------------
SPEC/FEATURES FOLDER
*install: gem 'capybara' --> if you need to
In spec_helper file:
require 'capybara/rails'
* require 'capybara/rails' --> spec_helper.rb
* mkdir spec/features
* touch spec/features/new_user_spec.rb
**To run the tests in the terminal -->
bundle exec rake --> THIS runs whatever the default rake tasks are - (including rspec) for your project (like Capybara tests)
rake -T --> shows your rake tasks
-------------------------------------------------------------------------
spec/features/new_user_spec.rb --> Capybara Spec File
-------------------------------------------------------------------------
require 'spec_helper'
describe 'the site' do
describe 'a new user visits the homepage' do
it 'displays a giant rat' do
#browser goes to rat
visit("/")
expect( page ).to have_content 'Cappy App'
end
it 'displays a giant rat' do
visit root_path
#temporary copy of page
save_and_open_page
expect( page.has_css?('img[alt="giant rat"]') ).to be_true
end
end
it 'has a sign-up link' do
visit root_path
click_link 'Sign Up'
expect(page).to have_content 'Please enter your name'
expect(current_path).to eq '/users/new'
end
describe "creating a user" do
# Given I've entered the correct info
# When I click on sign up
# Then I should go to the homepage
# And I should see "thanks for signing up"
describe 'signing up with valid credentials' do
let(:user) {FactoryGirl.build(:user)}
it 'takes us to the homepage and says thanks for signing up' do
sign_up(user)
expect(current_path).to eq root_path
expect(page).to have_content 'Thanks for signing up!'
end
end
describe 'doesn't let you sign up without email' do
let(:user) {FactoryGirl.build(:invalid_user)}
it 'doesn't let you sign up' do
sign_up(user)
expect(current_path).to eq new_user_path
expect(page).to have_content 'Please enter an email.'
end
end
end
end
* optional (below) creation of method to put in capybara file*
def sign_up(user)
visit root_path
click_link 'Sign Up'
fill_in 'Email', with: user.email
click_button 'Sign up!'
end
def login(user)
end
--------------------------------------------------------------------
FACTORY GIRL
----------------------------------------------------------------------
**You can use the gem 'factory_girl' to create testing objects:
Ex.
spec/factories/user_factory.rb
FactoryGirl.define do
factory :user do
email "[email protected]"
end
#need to specify b/c it's not 'user'
factory :invalid_user, class: User do
email nil
end
end
--------------------------------------------------------------------------------------
Jasmine Test Set Up (Javascript Tests --> Run on localhost:8888
--------------------------------------------------------------------------------------
1.) Add the gem 'jasmine' to your gemfile
bundle install
2.) rails g jasmine:install --> This will create the js spec files.
//jasmine_helper.rb --> This is usually where you run your tests.
rake jasmine ( sorta like running rspec)
i.e. bank_spec.js --> This file type goes in the 'spec/javascripts' folder
bank.js --> This file type goes in the app/assets/javascripts folder.
rake jasmine:ci --> 'continuous integration - don't run this one? Why?'
Now put tests into bank_spec.js OR jasmine_helper.rb?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment