Skip to content

Commit

Permalink
Add Article controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sk8higher committed Nov 9, 2023
1 parent b2ce397 commit 940cc53
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.6.4-x86_64-linux)
stimulus-rails (1.3.0)
railties (>= 6.0.0)
stringio (3.0.9)
Expand Down Expand Up @@ -352,6 +353,7 @@ DEPENDENCIES
simplecov (~> 0.17)
simplecov-lcov (~> 0.8.0)
sprockets-rails
sqlite3
stimulus-rails
turbo-rails
tzinfo-data
Expand Down
100 changes: 100 additions & 0 deletions spec/controllers/articles_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# frozen_string_literal: true

require_relative '../rails_helper'

RSpec.describe ArticlesController do
let(:saved_article) { create(:article) }
let(:created_article) { build(:article) }

describe 'GET #index' do
it 'returns http success and renders the index template' do
get :index
expect(response).to have_http_status(:success)
expect(response).to render_template(:index)
end
end

describe 'GET #show' do
it 'returns http success and renders the show template' do
get :show, params: { id: saved_article.id }
expect(response).to have_http_status(:success)
expect(response).to render_template(:show)
end
end

describe 'GET #new' do
it 'returns http success and renders the new template' do
get :new
expect(response).to have_http_status(:success)
expect(response).to render_template(:new)
end
end

describe 'POST #create' do
let(:file) { fixture_file_upload(Rails.root.join('spec/fixtures/image7.jpg'), 'image/jpg') }

it 'creates a new article and redirects to the show page' do
expect do
post :create, params: { article: { name_ru: created_article.name_ru,
name_en: created_article.name_en,
description_ru: created_article.description_ru,
description_en: created_article.description_en,
images: [file] } }
end.to change(Article, :count).by(1)
expect(response).to redirect_to(assigns(:article))
end

it 'returns unprocessable_entity if article is not saved' do
expect do
post :create, params: { article: { name_ru: nil,
description_ru: nil,
images: file } }
end.not_to change(Article, :count).from(0)

expect(response).to have_http_status(:unprocessable_entity)
expect(response).to render_template(:new)
end
end

describe 'GET #edit' do
it 'returns http success and renders the edit template' do
get :edit, params: { id: saved_article.id }
expect(response).to have_http_status(:success)
expect(response).to render_template(:edit)
end
end

describe 'PATCH #update' do
let(:file) { fixture_file_upload(Rails.root.join('spec/fixtures/image7.jpg'), 'image/jpg') }

it 'updates the article and redirects to the show page' do
new_description = Faker::Lorem.characters(number: 40)

patch :update, params: { id: saved_article.id, article: { name_ru: Faker::Lorem.characters(number: 40),
description_ru: new_description,
images: file } }
expect(response).to redirect_to(assigns(:article))
saved_article.reload
expect(saved_article.description_ru).to eq(new_description)
end

it 'returns unprocessable entity if article is not updated' do
patch :update, params: { id: saved_article.id, article: { name_ru: nil,
description_ru: nil,
images: file } }

expect(response).to have_http_status(:unprocessable_entity)
expect(response).to render_template(:edit)
end
end

describe 'DELETE #destroy' do
it 'deletes the article and redirects to the index page' do
delete_article = create(:article)
expect do
delete :destroy, params: { id: delete_article.id }
end.to change(Article, :count).by(-1)
expect(response).to redirect_to(articles_path)
end
end
end
Binary file added spec/fixtures/image7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 940cc53

Please sign in to comment.