Skip to content

Instantly share code, notes, and snippets.

@uriklar
Last active December 29, 2017 16:41
Show Gist options
  • Select an option

  • Save uriklar/7b5ebd24e27c8f8b10ef4d06170ff645 to your computer and use it in GitHub Desktop.

Select an option

Save uriklar/7b5ebd24e27c8f8b10ef4d06170ff645 to your computer and use it in GitHub Desktop.
Hub Eilat Web Course - Lesson 3 - Home assignment
1. Create the PostsController using the command: rails g controller Posts
2. Copy the attached file posts_controller_test.rb and paste it into test/controllers/posts_controller_test.rb
3. run tests using the command: rake test TEST=./test/controllers/posts_controller_test.rb
ื”ืงื•ื ื˜ืจื•ืœืจ ืฉืœื›ื ืฆืจื™ืš ืœื”ื›ื™ืœ 5 ืคื•ื ืงืฆื™ื•ืช (ื™ื›ื•ืœื™ื ืœื”ืขื–ืจ ื‘ื“ื•ื’ืžื ื”ืžืฆื•ืจืคืช users_controller.rb)
index:
ืžืงื‘ืœืช ื›ืคืจืžื˜ืจื™ื:
params[:user_id] - ื”ืื™ื™ ื“ื™ ืฉืœ ื”ื™ื•ื–ืจ ืฉืืช ื”ืคื•ืกื˜ื™ื ืฉืœื• ืื ื—ื ื• ืจื•ืฆื™ื ืœืงื‘ืœ
ืžื—ื–ื™ืจื”:
ืืช ื›ืœ ื”ืคื•ืกื˜ื™ื ืฉืœ ื”ื™ื•ื–ืจ ื”ื–ื”
show:
ืคืจืžื˜ืจื™ื
params[:user_id] - ืื™ื™ ื“ื™ ืฉืœ ื”ื™ื•ื–ืจ, params[:id] - ืื™ื™ ื“ื™ ืฉืœ ื”ืคื•ืกื˜ ื”ืžื‘ื•ืงืฉ
ืžื—ื–ื™ืจื”:
ืืช ื”ืคื•ืกื˜ ื”ืžื‘ื•ืงืฉ
create:
ืคืจืžื˜ืจื™ื:
params[:post][:text] - ื”ื˜ืงืกื˜ ืฉืœ ื”ืคื•ืกื˜ ืฉืžื‘ืงืฉื™ื ืœื™ืฆื•ืจ
params[:user_id] - ืื™ื™ ื“ื™ ืฉืœ ื”ื™ื•ื–ืจ ืฉืชื—ืชื™ื• ื™ื•ื•ืฆืจ ื”ืคื•ืกื˜
ืžื—ื–ื™ืจื”:
ืืช ื”ืคื•ืกื˜ ื‘ืžื™ื“ื” ื•ื”ื•ื ื ื•ืฆืจ ื‘ื”ืฆืœื—ื”
ืืช ื”ืฉื’ื™ืื•ืช ืฉืœ ื”ืคื•ืกื˜ ื‘ืžื™ื“ื” ื•ื”ื•ื ื ื›ืฉืœ
destroy:
ืคืจืžื˜ืจื™ื:
params[:id] - ืื™ื™ ื“ื™ ืฉืœ ื”ืคื•ืกื˜ ื”ืžื‘ื•ืงืฉ
ืžื—ื–ื™ืจื”:
ืžื•ื—ืงืช ืืช ื”ืคื•ืกื˜ ื”ืžื‘ื•ืงืฉ ื•ืžื—ื–ื™ืจื” ืื•ืชื•
search (ื™ื•ืชืจ ืžืืชื’ืจ)
ืžื‘ืงืœืช:
params[:user_id] - ื”ื™ื•ื–ืจ ืฉื‘ืคื•ืกื˜ื™ื ืฉืœื ื• ืื ื—ื ื• ืจื•ืฆื™ื ืœืงืคืฉ
params[:query] - ื˜ืงืกื˜ ืฉืื ื—ื ื• ืจื•ืฆื™ื ืœื—ืคืฉ ืคื•ืกื˜ื™ื ืฉืžื›ื™ืœื™ื ืื•ืชื•
ืžื—ื–ื™ืจื”:
ืืช ื›ืœ ื”ืคื•ืกื˜ื™ื ืฉืœ ื”ื™ื•ื–ืจ ื”ื–ื” ืฉืžื›ื™ืœื™ื ืืช ื”ื˜ืงืกื˜ ื”ื–ื”
ื”ืขืจื” ื—ืฉื•ื‘ื”:
ืœื ืžื•ื’ื“ืจ ื‘ืงื•ื‘ืฅ
routes.rb
ืจื•ื•ื˜ ื‘ืฉื‘ื™ืœ ื”ืคื•ื ืงืฆื™ื”
search.
ืืชื ืชืฆื˜ืจื›ื• ืœื”ื’ื“ื™ืจ ืื•ืชื• ื‘ืขืฆืžื›ื
#test/controllers/posts_controller_test.rb
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
setup do
@user = User.create({ first_name: "Donald", last_name: "Trump" })
@user.posts.create([
{ text: "Wow, โ€œFBI lawyer James Baker reassigned,โ€ according to @FoxNews" },
{ text: "Nobody but Donald Trump will save Israel"}
])
end
test "index action should return all of the user's posts" do
get :index, params: { user_id: @user.id }
res = get_json @response
assert_equal(2, res.count)
end
test "show action should return a specific post" do
get :show, params: { user_id: @user.id, id: @user.posts.first.id }
res = get_json @response
assert_equal(res["text"], @user.posts.first.text)
end
test "create action should create a post and return it" do
post_count_before = Post.count
post :create, params: { user_id: @user.id, post: { text: "Create this post!!" } }
res = get_json @response
assert_equal(post_count_before + 1, Post.count)
end
test "destroy action should delete a post and return it" do
post_count_before = Post.count
delete :destroy, params: { user_id: @user.id, id: @user.posts.last.id }
res = get_json @response
assert_equal(post_count_before - 1, Post.count)
end
test "search action should return posts that include query" do
get :search, params: { user_id: @user.id, query: "FBI" }
res = get_json @response
assert_equal(@user.posts.first.id, res[0]["id"])
end
end
# test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
def get_json response
JSON.parse response.body
end
end
# app/controllers/users_controller.rb
class UsersController < ApplicationController
# GET /users
def index
@users = User.all
render json: @users
end
# GET /users/1
def show
@user = User.find(params[:id])
render json: @user
end
# POST /users
def create
@user = User.new(user_params)
if @user.save
render json: @user
else
render json: @user.errors
end
end
# PATCH/PUT /users/1
def update
@user = User.find(params[:id])
if @user.update(user_params)
render json: @user
else
render json: @user.errors
end
end
# DELETE /users/1
def destroy
@user = User.find(params[:id])
@user.destroy
render json: @user
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment