Skip to content

Commit

Permalink
Rspec for product controller, update account controller
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhhung-131 committed Apr 4, 2024
1 parent f1da899 commit beeb1bc
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .sun-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ jobs:
MYSQL_ROOT_PASSWORD: password-test
script:
- sudo RAILS_ENV=test bundle exec rails db:drop db:create db:migrate
- bundle exec rspec
- sudo bundle exec rspec
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ruby "3.2.0"
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "bootstrap-sass", "3.4.1"
gem "byebug"
gem "connection_pool"
gem "image_processing", ">= 1.2"
gem "jquery-rails"
gem "owlcarousel-rails"
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ DEPENDENCIES
byebug
capybara
config
connection_pool
cssbundling-rails (~> 1.4)
debug
dotenv
Expand Down
19 changes: 0 additions & 19 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ def index
@product_outstandings = Product.product_outstanding
end

def new
@product = Product.new
end

def create
product = Product.new(product_params)
if product.save
flash[:success] = t("products.create.success")
redirect_to(products_path)
else
flash.now[:error] = t("products.create.error")
render(:new)
end
end

def show; end

private
Expand Down Expand Up @@ -53,8 +38,4 @@ def load_product
flash[:error] = t("products.load.error")
redirect_to(products_path)
end

def product_params
params.require(:product).permit(:name, :price, :image)
end
end
1 change: 1 addition & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# While tests run files are not watched, reloading is not necessary.
config.enable_reloading = false
config.active_job.queue_adapter = :inline

# Eager loading loads your entire application. When running a single test locally,
# this is usually not necessary, and can slow down your test suite. However, it's
Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Rails.application.routes.draw do
scope "(:locale)", locale: /en|vi/ do
root "products#index"
resources :products do
resources :products, only: %i(index show) do
resources :comments
end
get "/login", to: "sessions#new"
post "/login", to: "sessions#create"
get "/logout", to: "sessions#destroy"
resource :cart, only: %i[show create destroy update]
resources :orders, only: %i[create show index] do
resource :cart, only: %i(show create destroy update)
resources :orders, only: %i(create show index) do
member do
patch "/cancel", to: "orders#cancel"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,29 @@

describe "POST #create" do
context "with valid attributes" do
before { post :create, params: { account: attributes_for(:account) } }
let(:valid_params) do
{
account: {
name: "Test User",
email: "[email protected]",
address: "123 Test Street",
phone_number: "1234567890",
password: "password",
password_confirmation: "password"
}
}
end

before { post :create, params: valid_params }

it "creates a new account" do
expect(assigns(:account).id).to(be_present)
expect(Account.last).to(have_attributes(
name: "Test User",
email: "[email protected]",
address: "123 Test Street",
phone_number: "1234567890"
)
)
end

it "redirects to root path" do
Expand All @@ -33,11 +52,21 @@
end

context "with invalid attributes" do
before do
invalid_attributes = attributes_for(:account).merge(name: nil, email: "invalid_email", password: "123", password_confirmation: "123456")
post :create, params: { account: invalid_attributes }
let(:invalid_attributes) do
{
account: {
name: "",
email: "",
address: "",
phone_number: "",
password: "",
password_confirmation: ""
}
}
end

before { post :create, params: invalid_attributes }

it "does not save the new account" do
expect(assigns(:account).id).not_to(be_present)
end
Expand Down
53 changes: 53 additions & 0 deletions spec/controllers/products_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe(ProductsController, type: :controller) do
describe "GET #index" do
let!(:products) do
FactoryBot.create_list(:product, Settings.DIGIT_10)
end

before { get :index }

it "assigns @pagy and @products" do
expect(assigns(:pagy).count).to(eq(Settings.DIGIT_10))
products_per_page = Settings.PAGE_9
expect(assigns(:products).count).to(eq(products_per_page))
end

it "renders the index template" do
expect(response).to(render_template(:index))
end
end

describe "GET #show" do
context "when product exists" do
let!(:product) { FactoryBot.create(:product) }

before do
get :show, params: { id: product.id }
end

it "assigns the requested product to @product" do
expect(assigns(:product)).to(eq(product))
end

it "renders the show template" do
expect(response).to(render_template("show"))
end
end

context "when product does not exist" do
before { get :show, params: { id: 0 } }

it "flash error if product doesn't exist" do
expect(flash[:error]).to(eq(I18n.t("products.load.error")))
end

it "redirects to products_path if product is not found" do
expect(response).to(redirect_to(products_path))
end
end
end
end
2 changes: 1 addition & 1 deletion spec/factories/categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

FactoryBot.define do
factory :category do
name { Faker::Name.name }
name { %w(Apple Samsung Xiaomi Oppo Huawei Vivo).sample }
end
end
1 change: 1 addition & 0 deletions spec/factories/products.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
quantity { Faker::Number.between(from: 1, to: 100) }
category { association :category }
description { Faker::Lorem.paragraph }
image { Rack::Test::UploadedFile.new(File.open(Rails.root.join("spec/files/image_test.jpg").to_s)) }
end
end
Binary file added spec/files/image_test.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 beeb1bc

Please sign in to comment.