mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 11:47:56 +01:00
Add tests of controllers
This commit is contained in:
@@ -1,10 +1,7 @@
|
|||||||
class PostsController < ApplicationController
|
class PostsController < ApplicationController
|
||||||
before_action :authenticate_user!, only: [:create]
|
before_action :authenticate_user!, only: [:create]
|
||||||
|
|
||||||
def index_by_board_id
|
def index
|
||||||
board_id = params[:board_id] || 1
|
|
||||||
|
|
||||||
|
|
||||||
posts = Post
|
posts = Post
|
||||||
.left_outer_joins(:post_status)
|
.left_outer_joins(:post_status)
|
||||||
.select('posts.title, posts.description, post_statuses.name as post_status_name, post_statuses.color as post_status_color')
|
.select('posts.title, posts.description, post_statuses.name as post_status_name, post_statuses.color as post_status_color')
|
||||||
@@ -28,7 +25,11 @@ class PostsController < ApplicationController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def filter_params
|
def filter_params
|
||||||
params.permit(:board_id, :post_status_id)
|
defaults = { board_id: Board.first.id }
|
||||||
|
|
||||||
|
params
|
||||||
|
.permit(:board_id, :post_status_id)
|
||||||
|
.with_defaults(defaults)
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_params
|
def post_params
|
||||||
@@ -37,5 +38,4 @@ class PostsController < ApplicationController
|
|||||||
.permit(:title, :description, :board_id)
|
.permit(:title, :description, :board_id)
|
||||||
.merge(user_id: current_user.id)
|
.merge(user_id: current_user.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ Rails.application.routes.draw do
|
|||||||
devise_for :users
|
devise_for :users
|
||||||
|
|
||||||
resources :boards, only: [:show]
|
resources :boards, only: [:show]
|
||||||
|
resources :posts, only: [:index, :create]
|
||||||
post '/posts', to: 'posts#create'
|
resources :post_statuses, only: [:index]
|
||||||
get '/posts', to: 'posts#index_by_board_id'
|
|
||||||
get '/post_statuses', to: 'post_statuses#index'
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
require 'rails_helper'
|
|
||||||
|
|
||||||
RSpec.describe PostsController, type: :controller do
|
|
||||||
|
|
||||||
end
|
|
||||||
25
spec/requests/post_statuses_controller_test.rb
Normal file
25
spec/requests/post_statuses_controller_test.rb
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe 'post statuses controller' do
|
||||||
|
let(:post_status1) { FactoryBot.create(:post_status) }
|
||||||
|
let(:post_status2) { FactoryBot.create(:post_status) }
|
||||||
|
|
||||||
|
def create_post_statuses
|
||||||
|
post_status1
|
||||||
|
post_status2
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'index' do
|
||||||
|
before(:each) { create_post_statuses }
|
||||||
|
|
||||||
|
it 'returns all post statuses as JSON' do
|
||||||
|
get post_statuses_path
|
||||||
|
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(json.length).to eq(2)
|
||||||
|
expect(json[0]['order']).to be <= json[1]['order']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
100
spec/requests/posts_controller_test.rb
Normal file
100
spec/requests/posts_controller_test.rb
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe 'posts controller', type: :request do
|
||||||
|
let (:user) { FactoryBot.create(:user) }
|
||||||
|
|
||||||
|
let(:board1) { FactoryBot.create(:board) }
|
||||||
|
let(:board2) { FactoryBot.create(:board) }
|
||||||
|
|
||||||
|
let(:post1) { FactoryBot.create(:post, board: board1) }
|
||||||
|
let(:post2) { FactoryBot.create(:post, board: board1) }
|
||||||
|
let(:post3) { FactoryBot.create(:post, board: board2) }
|
||||||
|
|
||||||
|
def create_posts
|
||||||
|
post1
|
||||||
|
post2
|
||||||
|
post3
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'index' do
|
||||||
|
before(:each) { create_posts }
|
||||||
|
|
||||||
|
it 'returns posts of first board (if no board id provided) as JSON' do
|
||||||
|
get posts_path
|
||||||
|
json1 = JSON.parse(response.body)
|
||||||
|
|
||||||
|
get posts_path(board_id: Board.first.id)
|
||||||
|
json2 = JSON.parse(response.body)
|
||||||
|
|
||||||
|
expect(json1).to eq(json2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns posts filtered by board id as JSON' do
|
||||||
|
get posts_path(board_id: post1.board_id)
|
||||||
|
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(json.length).to eq(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns posts filtered by post status id as JSON' do
|
||||||
|
get posts_path(board_id: post1.board_id, post_status_id: post1.post_status_id)
|
||||||
|
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(json.length).to eq(1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'create' do
|
||||||
|
before(:each) do
|
||||||
|
user.confirm
|
||||||
|
sign_in user
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates a new post' do
|
||||||
|
post_count = Post.count
|
||||||
|
|
||||||
|
new_post = FactoryBot.build(:post, board: board1)
|
||||||
|
|
||||||
|
post(
|
||||||
|
posts_path,
|
||||||
|
params: {
|
||||||
|
post: {
|
||||||
|
title: new_post.title,
|
||||||
|
description: new_post.description,
|
||||||
|
board_id: new_post.board_id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(Post.count).to eq(post_count + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'redirects if not logged in' do
|
||||||
|
sign_out user
|
||||||
|
|
||||||
|
post_count = Post.count
|
||||||
|
|
||||||
|
new_post = FactoryBot.build(:post, board: board1)
|
||||||
|
|
||||||
|
post(
|
||||||
|
posts_path,
|
||||||
|
params: {
|
||||||
|
post: {
|
||||||
|
title: new_post.title,
|
||||||
|
description: new_post.description,
|
||||||
|
board_id: new_post.board_id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:redirect)
|
||||||
|
expect(response).to redirect_to(new_user_session_path)
|
||||||
|
expect(Post.count).to eq(post_count)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe 'board routing', :aggregate_failures, type: :routing do
|
RSpec.describe 'boards routing', :aggregate_failures, type: :routing do
|
||||||
it 'only routes the page to show a board' do
|
it 'routes boards' do
|
||||||
expect(get: '/boards/1').to route_to(
|
expect(get: '/boards/1').to route_to(
|
||||||
controller: 'boards', action: 'show', id: '1'
|
controller: 'boards', action: 'show', id: '1'
|
||||||
)
|
)
|
||||||
17
spec/routing/post_routing_spec.rb
Normal file
17
spec/routing/post_routing_spec.rb
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe 'posts routing', :aggregate_failures, type: :routing do
|
||||||
|
it 'routes posts' do
|
||||||
|
expect(get: '/posts').to route_to(
|
||||||
|
controller: 'posts', action: 'index'
|
||||||
|
)
|
||||||
|
expect(post: '/posts').to route_to(
|
||||||
|
controller: 'posts', action: 'create'
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(get: '/posts/1').not_to be_routable
|
||||||
|
expect(get: '/posts/1/edit').not_to be_routable
|
||||||
|
expect(patch: '/posts/1').not_to be_routable
|
||||||
|
expect(delete: '/posts/1').not_to be_routable
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user