Skip to content

Instantly share code, notes, and snippets.

@catm705
Last active March 24, 2021 04:01
Show Gist options
  • Save catm705/10011321 to your computer and use it in GitHub Desktop.
Save catm705/10011321 to your computer and use it in GitHub Desktop.
Steps for Rails w/Rspec App (initlal - no CRUD, just for :development, :test)
----------------------------------------------------------------------
#### 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 'rails-erd'
gem 'rspec-rails', '~> 3.0.0.beta'
gem 'jasmine'
gem 'shoulda-matchers'
gem 'factory_girl_rails'
gem 'rubocop'
gem 'annotate'
gem 'selenium-webdriver' # Need firefox for this to work.
# gem 'guard'
# gem 'guard-rspec'
# gem 'guard-bundler', require: false
# gem 'terminal-notifier-guard'
# gem 'nyan-cat-formatter'
end
5. bundle install
---------------------------------------------------------------------------------------
#### If you want to create a new GIT repo + ADD travis-ci.org #####
--------------------------------------------------------------------------------------
1.) cd into your app
2.) git init --> from inside your folder
3.) Create the new repo on Github
(Below should show up after creating new Repo)
* git remote add origin git@github.com:catm705/registry_app.git
* git push -u origin master
4.) -- May need to do this first to add the remote --
* git add .
* git commit -m "First commit"
6.) settings/webhooks --> add webhook (https://travis-ci.org/).
- This takes snapshots and installs it on a vm and lets us know if it worked or not. For FREE!
- In travis-ci.org --> Turn 'ON' your app by going into 'settings' and 'syncing' your repos.
Your Github is linked to travis-ci.org.
- touch .travis.yml file to your root directory. .travis.yml -->
(Put below code in there)
-------------------------
language: ruby
rvm:
- 2.1.0
before_script:
- "sh -e /etc/init.d/xvfb start"
- "export DISPLAY=:99.0"
- "export JASMINE_SPEC_FORMAT=documentation"
env:
- PG_USERNAME='postgres'
script:
- "bundle exec rake db:create"
- "bundle exec rake db:migrate RAILS_ENV=test"
- "bundle exec rake spec"
- "bundle exec rake jasmine:ci"
7.) Then git add, commit, and push **Now whenever you push to 'Github' the travis file should run tests.
8.) Go to Travis-ci.org site, goto 'build' icon in top right corner, then copy 'Markdown' code:
- Put this in your README.md file.
*** REMEMBER --> Your Travis TEST will only run the LAST push that you committed to Git *****
**** Any changes - You must add, commit, and PUSH AGAIN!!! It takes a while. *******
------------------------------------------------------------------------
#### IF you want 'GUARD' #######
----------------------------------------------------------------------
1.) bundle exec guard init → #will look to see what guard files you have
2.) 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??
3.) 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
----EXAMPLE CODE:-----
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 --> RAKE DB: ####
----------------------------------------------------------------------
rails g migration create_users
t.string :name
t.string :email
t.string :phone_number
t.timestamps
rake db:migrate RAILS_ENV=test
###### FYI: about rake db: #########
db:migrate --> runs (single) migrations that have not run yet.
db:create --> creates the database
db:drop --> deletes the database
db:schema:load --> creates tables and columns within the (existing) database following schema.rb
db:setup --> does db:create, db:schema:load, db:seed
db:reset --> does db:drop, db:setup (This is 1 step that all encompasses)
----------------------------------------------------------------------
#### 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 ####
-------------------------------------------------------------------------
----------- Example#1 Capybrara Code: -------------
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
------------ Example#2 Capybara Code:--------------
require 'spec_helper'
describe "thanks_gerald_app" do
describe 'a new user visits the homepage' do
it 'displays hey macarena' do
visit("/")
expect( page ).to have_content 'Hey Macarena'
end
end
describe 'a user clicks the headline', js: true do
it 'changes the message to Hey Gerald' do
visit root_path
page.find("h1", :text => 'Hey Macarena').click
expect( page ).to have_content 'Hey Gerald'
end
end
describe '#formal name' do
it "shows Mr. for Men" do
person1 = Person.create(first_name: "Ragnar", last_name: "Lothbrok", age: 27, gender: "male", is_married: false)
expect( person1.formal_name ).to eq("Mr. Ragnar Lothbrok")
end
it "shows Mrs. for Men" do
person1 = Person.create(first_name: "Katniss", last_name: "Everdeen", age: 27, gender: "female", is_married: false)
expect( person1.formal_name ).to eq("Mrs. Katniss Everdeen")
end
end
end
--------------------------------------------------------------------
#### FACTORY GIRL ####
----------------------------------------------------------------------
**You can use the gem 'factory_girl' to create testing objects:
---Example Code:---
spec/factories/user_factory.rb
FactoryGirl.define do
factory :user do
email "phil@example.com"
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???? Not sure...
--------------------------------------------------------------------------------------
Removing Turbolinks - so AJAX calls don't get messed with.
--------------------------------------------------------------------------------------
Remove the gem 'turbolinks' line from your Gemfile.
Remove the //= require turbolinks from your app/assets/javascripts/application.js.
Remove the two "data-turbolinks-track" => true hash key/value pairs from your app/views/layouts/application.html.erb.
--------------------------------------------------------------------------------------
SimpleCov Gem
--------------------------------------------------------------------------------------
** add to gemfile
gem 'simplecov', '~> 0.7.1', :require => false
(At the top of the spec_helper file)
require 'simplecov'
SimpleCov.start 'rails'
open coverage/index.html --> from terminal --> To see how it's going in the browser.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment