From 3e106dc7f5fbd6961c67f41aea66033c27b5142f Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 18 Mar 2023 12:15:43 -0600 Subject: AƱade spec de products_controller#show MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/products_controller.rb | 16 +++++++++------- app/models/product.rb | 8 -------- spec/controllers/products_controller_spec.rb | 14 +++++++++----- spec/factories/product.rb | 2 +- spec/models/product_spec.rb | 7 +++++++ .../products_controller/show_products_controller_spec.rb | 16 ++++++++++++++++ 6 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 spec/requests/products_controller/show_products_controller_spec.rb diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index ec82af6..0c3a30b 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -4,9 +4,7 @@ module Api # ProductsController class ProductsController < MasterController def show - @product = Product.find_by(public_id: params[:id]) - - render json: not_found_error_message, status: :not_found and return if @product.nil? + render json: not_found_error_message, status: :not_found and return if product.nil? render json: serialized_object.serializable_hash, status: :ok end @@ -48,11 +46,15 @@ module Api private + def product + @product ||= Product.joins(:company) + .select('products.*', 'companies.name as company_name', + 'companies.short_name as company_short_name') + .find_by(public_id: params[:id]) + end + def serialized_object - Serializers::ProductSerializer.new( - @product.joins(:company) - .select('products.*', 'companies.name as company_name','companies.short_name as company_short_name') - ) + Serializers::ProductSerializer.new(product) end def serialized_collection diff --git a/app/models/product.rb b/app/models/product.rb index 6e76f75..e6f3720 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -21,17 +21,9 @@ class Product < ApplicationRecord has_one_attached :picture - # around_create :generate_public_id - serialize :categories, Array def to_param public_id end - - #private - - #def generate_public_id - # self.public_id = SecureRandom.hex(12) - #end end diff --git a/spec/controllers/products_controller_spec.rb b/spec/controllers/products_controller_spec.rb index d4f1373..aabab4b 100644 --- a/spec/controllers/products_controller_spec.rb +++ b/spec/controllers/products_controller_spec.rb @@ -3,11 +3,15 @@ require 'rails_helper' RSpec.describe Api::ProductsController, type: :controller do - let(:product) { create(:product) } - it { should route(:post, '/api/products').to(action: :create) } - it { should route(:put, "/api/products#{product.public_id}").to(action: :update, id: product.public_id) } + it do + should route(:put, '/api/products/0439733420a4c5a3e8239bb1').to(action: :update, id: '0439733420a4c5a3e8239bb1') + end it { should route(:get, '/api/products').to(action: :index) } - it { should route(:get, "/api/products/#{product.public_id}").to(action: :show, id: product.public_id) } - it { should route(:delete, "/api/products/#{product.public_id}").to(action: :destroy, id: product.public_id) } + it do + should route(:get, '/api/products/0439733420a4c5a3e8239bb1').to(action: :show, id: '0439733420a4c5a3e8239bb1') + end + it do + should route(:delete, '/api/products/0439733420a4c5a3e8239bb1').to(action: :destroy, id: '0439733420a4c5a3e8239bb1') + end end diff --git a/spec/factories/product.rb b/spec/factories/product.rb index ab65cd0..abdf4bc 100644 --- a/spec/factories/product.rb +++ b/spec/factories/product.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true FactoryBot.define do - factory :company, class: 'Company' do + factory :product, class: 'Product' do name { Faker::Commerce.product_name } unitary_price { rand(1000.0) } bulk_price { rand(1000.0) } diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index b3790fe..d9d0c64 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -10,4 +10,11 @@ RSpec.describe Product, type: :model do it { should have_one_attached(:picture) } it { should belong_to(:company) } it { should serialize(:categories) } + + describe '#to_param' do + it "describes the param the product's routes expect" do + product = create(:product, public_id: SecureRandom.hex(12)) + expect(product.to_param).to eq(product.public_id) + end + end end diff --git a/spec/requests/products_controller/show_products_controller_spec.rb b/spec/requests/products_controller/show_products_controller_spec.rb new file mode 100644 index 0000000..3ef5148 --- /dev/null +++ b/spec/requests/products_controller/show_products_controller_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/products/:public_id', type: :request do + it_behaves_like 'a GET resource request' do + let(:headers) { {} } + let(:product) { create(:product, public_id: SecureRandom.hex(12)) } + let(:route) { "/api/products/#{product.public_id}" } + let(:invalid_route) { "/api/products/#{Faker::Lorem.word}" } + let(:expected_text) do + product.categories.concat([product.name, product.id.to_s, product.unitary_price.to_s, product.bulk_price.to_s, + product.available_quantity.to_s, product.company.name, product.company.short_name]) + end + end +end -- cgit v1.2.3