Created
May 29, 2015 20:29
-
-
Save novohispano/61f08d14d40caa3d51d0 to your computer and use it in GitHub Desktop.
Rails Testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item < ActiveRecord::Base | |
validates :name, presence: true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ItemsController < ApplicationController | |
def show | |
@item = Item.find(params[:id]) | |
end | |
def index | |
@items = Item.all | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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