diff --git a/Gemfile.lock b/Gemfile.lock index df371e2..b1f399d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -352,6 +353,7 @@ DEPENDENCIES simplecov (~> 0.17) simplecov-lcov (~> 0.8.0) sprockets-rails + sqlite3 stimulus-rails turbo-rails tzinfo-data diff --git a/spec/controllers/articles_controller_spec.rb b/spec/controllers/articles_controller_spec.rb new file mode 100644 index 0000000..c15d200 --- /dev/null +++ b/spec/controllers/articles_controller_spec.rb @@ -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 diff --git a/spec/fixtures/image7.jpg b/spec/fixtures/image7.jpg new file mode 100644 index 0000000..991896a Binary files /dev/null and b/spec/fixtures/image7.jpg differ