Skip to content

Instantly share code, notes, and snippets.

@novohispano
Created May 29, 2015 20:29
Show Gist options
  • Save novohispano/61f08d14d40caa3d51d0 to your computer and use it in GitHub Desktop.
Save novohispano/61f08d14d40caa3d51d0 to your computer and use it in GitHub Desktop.
Rails Testing
class Item < ActiveRecord::Base
validates :name, presence: true
end
require 'test_helper'
class ItemTest < ActiveSupport::TestCase
test 'it creates an item' do
item = Item.create(name: 'hat', description: 'this is a hat', price: 10)
assert_equal 'hat', item.name
assert_equal 'this is a hat', item.description
assert_equal 10, item.price
end
test 'it cannot create an item without a name' do
item = Item.new(description: 'this is a hat', price: 10)
refute item.valid?, 'Expected to be invalid, but was valid.'
end
end
class ItemsController < ApplicationController
def show
@item = Item.find(params[:id])
end
def index
@items = Item.all
end
end
require 'test_helper'
class ItemsControllerTest < ActionController::TestCase
test '#show' do
item = Item.create(name: 'hat', description: 'this is a hat', price: 100)
get :show, id: item.id
assert_response :success
assert_not_nil assigns(:item)
end
test '#index' do
item_1 = Item.create(name: 'glass', description: 'this is a glass', price: 100)
item_2 = Item.create(name: 'hat', description: 'this is a hat', price: 100)
get :index
assert_response :success
assert_not_nil assigns(:items)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment