Add tests of controllers

This commit is contained in:
riggraz
2019-09-04 17:37:08 +02:00
parent 542bbcfb85
commit 2a42d3069c
7 changed files with 152 additions and 17 deletions

View File

@@ -1,5 +0,0 @@
require 'rails_helper'
RSpec.describe PostsController, type: :controller do
end

View 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

View 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

View File

@@ -1,7 +1,7 @@
require 'rails_helper'
RSpec.describe 'board routing', :aggregate_failures, type: :routing do
it 'only routes the page to show a board' do
RSpec.describe 'boards routing', :aggregate_failures, type: :routing do
it 'routes boards' do
expect(get: '/boards/1').to route_to(
controller: 'boards', action: 'show', id: '1'
)

View 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