Skip to content

Instantly share code, notes, and snippets.

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'devise'
@AttyC
AttyC / application.html.erb
Last active September 19, 2015 08:06
Add Lettering.js plugin to Rails app
<!DOCTYPE html>
<html>
<head>
<title>Workspace</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<script>Stripe.setPublishableKey("<%= Rails.configuration.stripe[:publishable_key] %>");</script>
<%= csrf_meta_tags %>
</head>
@AttyC
AttyC / _stripe_checkout_button.html.erb
Created September 11, 2015 09:42
Stripe - do I need Stripe.js?
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-image="/square-image.png"
data-name="Demo Site"
data-description="data..."
data-amount="2000">
</script>
@AttyC
AttyC / _product.html.erb
Created September 11, 2015 09:12
comment path undefined
<tr>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.image_url %></td>
<td><%= product.colour %></td>
<td><%= link_to 'Show', product %></td>
<td><% if signed_in? && current_user.admin? %>
<%= link_to 'Edit', edit_product_path(product) %>
<% end %>
</td>
@AttyC
AttyC / _product.html.erb
Created September 11, 2015 09:12
comment path undefined
<tr>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.image_url %></td>
<td><%= product.colour %></td>
<td><%= link_to 'Show', product %></td>
<td><% if signed_in? && current_user.admin? %>
<%= link_to 'Edit', edit_product_path(product) %>
<% end %>
</td>
@AttyC
AttyC / static_pages_controller_spec.rb
Created September 8, 2015 12:34
Functional tests User Controller
require 'rails_helper'
describe StaticPagesController, :type => :controller do
describe "GET #index" do
it "responds successfully with an HTTP 200 status code" do
get :index
expect(response).to be_success
expect(response).to have_http_status(200)
end
@AttyC
AttyC / product_spec.rb
Created September 8, 2015 12:08
Unite test - average_rating SUCCESS
require 'rails_helper' # also requires spec_helper and adds other stuff - if didnt need Rails just use spec_helper but unlikely
describe Product do
describe "#average_rating" do # - the '#' signifies that we are testing an instance method - describe the method you will be testing (which belongs to the Class)
#context 1, test 1
context "when the product has comments" do # create context
before do # before running the test...
@product = Product.create(:name => "ball")
@AttyC
AttyC / error - not averaging
Last active September 7, 2015 18:44
unit test failing
attyc@appcity:~/workspace (comments) $ rspec
F
Failures:
1) Product#average_rating when the product has comments returns the average rating of all comments
Failure/Error: @product.comments.create!(:rating => 1, :user_id => 10, :body => "hello")
ActiveRecord::RecordInvalid:
Validation failed: User can't be blank
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/validations.rb:79:in `raise_record_invalid'
@AttyC
AttyC / product.rb
Last active September 5, 2015 13:06
Unit test - average_rating
class Product < ActiveRecord::Base
has_many :orders
has_many :comments
def average_rating
comments.average(:rating).to_f
end
validates :name, presence: true
end
Rails.application.routes.draw do
get 'static_pages/about'
get 'static_pages/contact'
get 'static_pages/index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".