Skip to content

Instantly share code, notes, and snippets.

@chrisdpeters
Last active December 15, 2015 14:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdpeters/461e9237375d22fff533 to your computer and use it in GitHub Desktop.
Save chrisdpeters/461e9237375d22fff533 to your computer and use it in GitHub Desktop.
Introduction to RSpec feature specs
require 'rails_helper'
feature 'Admin creates product' do
...
scenario 'with invalid input' do
visit admin_products_path
click_link 'New Product'
click_button 'Create Product'
expect(page).to have_content 'There was an error creating the product'
end
end
require 'rails_helper'
feature 'Admin creates product' do
given!(:product) { Product.new(title: 'Widget X', description: 'This is a description.', price: 12.99, shipping_price: 2.95) }
scenario 'with valid input' do
visit admin_products_path
click_link 'New Product'
fill_in 'Title', with: product.title
fill_in 'Description', with: product.description
fill_in 'Price', with: product.price
fill_in 'Shipping price', with: product.shipping_price
click_button 'Create Product'
expect(page).to have_content 'The product was created successfully'
end
end
require 'rails_helper'
feature 'Admin deletes product' do
given!(:product) { Product.create!(title: 'Widget X', description: 'This is a description.', price: 12.99, shipping_price: 2.95) }
scenario do
visit admin_products_path
click_link 'Delete'
expect(page).to have_content 'The product was deleted successfully'
expect(page).to_not have_content product.title
end
end
require 'rails_helper'
feature 'Admin updates product' do
given!(:product) { Product.create!(title: 'Widget X', description: 'This is a description.', price: 12.99, shipping_price: 2.95) }
given(:new_product) { Product.new(title: 'Widget Y', description: 'This is another description.', price: 24.95, shipping_price: 2.95) {
scenario 'with valid input' do
visit admin_products_path
click_link product.title
fill_in 'Title', with: new_product.title
fill_in 'Description', with: new_product.description
fill_in 'Price', with: new_product.price
fill_in 'Shipping price', with: new_product.shipping_price
click_button 'Update Product'
expect(page).to have_content 'The product was updated successfully'
end
scenario 'with invalid input' do
visit admin_products_path
click_link product.title
fill_in 'Title', with: ''
fill_in 'Description', with: ''
fill_in 'Price', with: ''
fill_in 'Shipping price', with: ''
click_button 'Update Product'
expect(page).to have_content 'There was an error updating the product'
end
end
<% flash.each do |key, message| %>
<div id="flash-<%= key %>" class="flash-<%= key %>">
<%= message %>
</div>
<% end %>
<%= yield %>
def create
@product = Product.new(product_params)
if @product.save
redirect_to edit_admin_product_path(@product), notice: 'The product was created successfully.'
else
flash[:error] = 'There was an error creating the product.'
render :new
end
end
$ rails g model product title:string description:text price_cents:integer shipping_price_cents:integer
$ rake db:migrate
<h1>Edit Product</h1>
<%= render 'form' %>
$ rspec
F
Failures:
1) Admin creates product with valid input
Failure/Error: expect(page).to have_content 'The product was created successfully'
expected to find text "The product was created successfully" in "Edit Product * Title * DescriptionThis is a description. Price Shipping price"
# ./spec/features/products/admin_creates_product_spec.rb:14:in `block (2 levels) in <top (required)>'
Finished in 1.01 seconds (files took 1.4 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/features/products/admin_creates_product_spec.rb:6 # Admin creates product with valid input
<%= simple_form_for [:admin, @product] do |f| %>
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :price %>
<%= f.input :shipping_price %>
<%= f.submit %>
<% end %>
<h1>Products</h1>
<p><%= link_to 'New Product', new_admin_product_path %></p>
<% if @products.any? %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th>Shipping</th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.title %></td>
<td><%= number_to_currency product.price %></td>
<td><%= number_to_currency product.shipping_price %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<h1>New Product</h1>
<%= render 'form' %>
$ rspec
.
Finished in 0.11697 seconds (files took 1.4 seconds to load)
1 example, 0 failures
class Admin::ProductsController < ApplicationController
def index
@products = Product.order(:title)
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to edit_admin_product_path(@product), notice: 'The product was created successfully.'
end
end
def edit
@product = Product.find(params[:id])
end
private
def product_params
params.require(:product).permit(:title, :description, :price, :shipping_price)
end
end
$ rspec
..
Finished in 0.15502 seconds (files took 1.43 seconds to load)
2 examples, 0 failures
scenario 'with JavaScript', js: true do
visit product_path(product)
click_link 'View Larger'
within '.modal-window' do
expect(page).to have_css 'video'
end
end
class Product < ActiveRecord::Base
# money-rails gem
monetize :price_cents
monetize :shipping_price_cents
validates :title, presence: true
validates :description, presence: true
end
Rails.application.routes.draw do
namespace :admin do
resources :products
end
end
require 'rails_helper'
feature 'User resends unlock instructions' do
... # given! statements to setup data would go here
scenario 'with valid input' do
visit admin_root_path
click_link "Didn't receive unlock instructions?"
fill_in 'Email', with: user.email
click_button 'Send Instructions'
expect(page).to have_content 'You will receive an email with unlock instructions'
expect(unread_emails_for(user.email).size).to eql 1
open_email user.email
expect(current_email).to have_subject 'Unlock Instructions'
expect(current_email).to have_body_text 'Your account has been locked'
click_email_link_matching /#{unlock_path(user.id)}/
expect(page).to have_content 'Your account has been unlocked successfully'
end
end
require 'rails_helper'
feature 'User signs in' do
given!(:user) { FactoryGirl.create(:user) }
scenario 'with valid credentials' do
visit root_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
check 'Remember me'
click_button 'Sign in'
expect(page).to have_content "Welcome back, #{user.first_name}!"
end
end
within '.heading' do
expect(page).to have_content 'Welcome back'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment