Skip to content

Instantly share code, notes, and snippets.

@uriklar
Last active December 29, 2017 16:41
Show Gist options
  • Save uriklar/7b5ebd24e27c8f8b10ef4d06170ff645 to your computer and use it in GitHub Desktop.
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