Skip to content

Commit

Permalink
Merge pull request #15 from Lucash2022/testing
Browse files Browse the repository at this point in the history
Create unit and integration tests for all the most important components of your RoR application
  • Loading branch information
Lucas-Erkana authored Jul 11, 2023
2 parents 9cc32d8 + 7b6aa5e commit fdde8a7
Show file tree
Hide file tree
Showing 21 changed files with 290 additions and 482 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
19 changes: 18 additions & 1 deletion spec/models/category_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
require 'rails_helper'

RSpec.describe Category, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let(:user) { User.create(name: 'Lucas Erkana', email: 'invalid_email', password: 'password') }
subject do
Category.new(name: 'Food', icon: 'food.jpng', author: user)
end

it 'should not be valid with name value set to nil' do
subject.name = nil
expect(subject).to_not be_valid
end

it 'should not be valid with icon value set to nil' do
subject.icon = nil
expect(subject).to_not be_valid
end

it 'should be valid ' do
expect(subject).to be_valid
end
end
19 changes: 18 additions & 1 deletion spec/models/entity_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
require 'rails_helper'

RSpec.describe Entity, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let(:user) { User.create(name: 'John Doe') }
subject do
Entity.new(name: 'Food', amount: 10, author: user)
end

it 'should not be valid with name value is nil' do
subject.name = nil
expect(subject).to_not be_valid
end

it 'should not be valid if amount is less than 0' do
subject.amount = -5
expect(subject).to_not be_valid
end

it 'Should be valid' do
expect(subject).to be_valid
end
end
26 changes: 25 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
require 'rails_helper'

RSpec.describe User, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
describe 'validations' do
it 'validates presence of name' do
user = User.new(email: 'test@example.com', password: 'password')
expect(user).not_to be_valid
expect(user.errors[:name]).to include("can't be blank")
end

it 'validates presence of email' do
user = User.new(name: 'John Doe', password: 'password')
expect(user).not_to be_valid
expect(user.errors[:email]).to include("can't be blank")
end

it 'validates format of name' do
user = User.new(name: '', email: 'test@example.com', password: 'password')
expect(user).not_to be_valid
expect(user.errors[:name]).to include("can't be blank")
end

it 'validates format of email' do
user = User.new(name: 'John Doe', email: 'invalid_email', password: 'password')
expect(user).not_to be_valid
expect(user.errors[:email]).to include('is invalid')
end
end
end
63 changes: 63 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
abort e.to_s.strip
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, type: :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://rspec.info/features/6-0/rspec-rails
config.infer_spec_type_from_file_location!

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
128 changes: 8 additions & 120 deletions spec/requests/categories_spec.rb
Original file line number Diff line number Diff line change
@@ -1,130 +1,18 @@
require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to test the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.

RSpec.describe '/categories', type: :request do
# This should return the minimal set of attributes required to create a valid
# Category. As you add validations to Category, be sure to
# adjust the attributes here as well.
let(:valid_attributes) do
skip('Add a hash of attributes valid for your model')
end

let(:invalid_attributes) do
skip('Add a hash of attributes invalid for your model')
end

describe 'GET /index' do
it 'renders a successful response' do
Category.create! valid_attributes
get categories_url
expect(response).to be_successful
end
end

describe 'GET /show' do
it 'renders a successful response' do
category = Category.create! valid_attributes
get category_url(category)
expect(response).to be_successful
end
end
include Devise::Test::IntegrationHelpers

describe 'GET /new' do
it 'renders a successful response' do
get new_category_url
expect(response).to be_successful
end
end

describe 'GET /edit' do
it 'renders a successful response' do
category = Category.create! valid_attributes
get edit_category_url(category)
expect(response).to be_successful
end
end

describe 'POST /create' do
context 'with valid parameters' do
it 'creates a new Category' do
expect do
post categories_url, params: { category: valid_attributes }
end.to change(Category, :count).by(1)
end

it 'redirects to the created category' do
post categories_url, params: { category: valid_attributes }
expect(response).to redirect_to(category_url(Category.last))
end
end

context 'with invalid parameters' do
it 'does not create a new Category' do
expect do
post categories_url, params: { category: invalid_attributes }
end.to change(Category, :count).by(0)
end

it "renders a response with 422 status (i.e. to display the 'new' template)" do
post categories_url, params: { category: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

describe 'PATCH /update' do
context 'with valid parameters' do
let(:new_attributes) do
skip('Add a hash of attributes valid for your model')
end

it 'updates the requested category' do
category = Category.create! valid_attributes
patch category_url(category), params: { category: new_attributes }
category.reload
skip('Add assertions for updated state')
end

it 'redirects to the category' do
category = Category.create! valid_attributes
patch category_url(category), params: { category: new_attributes }
category.reload
expect(response).to redirect_to(category_url(category))
end
end

context 'with invalid parameters' do
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
category = Category.create! valid_attributes
patch category_url(category), params: { category: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

describe 'DELETE /destroy' do
it 'destroys the requested category' do
category = Category.create! valid_attributes
expect do
delete category_url(category)
end.to change(Category, :count).by(-1)
before do
@user = User.create(name: 'Lucas', email: 'lderkana21x@gmail.com', password: '123123',
password_confirmation: '123123')
sign_in @user
get authenticated_root_path
end

it 'redirects to the categories list' do
category = Category.create! valid_attributes
delete category_url(category)
expect(response).to redirect_to(categories_url)
it 'response to html' do
expect(response.content_type).to include 'text/html'
end
end
end
Loading

0 comments on commit fdde8a7

Please sign in to comment.