tap/spec/controllers/products_controller_spec.rb

172 lines
4.3 KiB
Ruby
Raw Normal View History

2015-10-29 13:53:11 +01:00
# barcode_products GET /products/barcode(.:format) products#barcode
# load_barcode_products POST /products/barcode(.:format) products#load_barcode
# products GET /products(.:format) products#index
# POST /products(.:format) products#create
# edit_product GET /products/:id/edit(.:format) products#edit
# product PATCH /products/:id(.:format) products#update
# PUT /products/:id(.:format) products#update
2015-09-22 07:43:31 +02:00
#
2015-09-07 14:26:07 +02:00
describe ProductsController, type: :controller do
before :each do
@admin = create :admin
sign_in @admin
end
2015-12-01 20:02:08 +01:00
############
# CREATE #
############
2015-09-22 07:43:31 +02:00
2015-09-07 14:26:07 +02:00
describe 'POST create' do
2015-12-01 20:02:08 +01:00
context 'successful' do
2015-09-07 14:26:07 +02:00
it 'should create a product' do
expect{
post :create, product: attributes_for(:product)
2015-09-18 16:49:41 +02:00
}.to change{ Product.count }.by(1)
2015-09-07 14:26:07 +02:00
end
2015-10-29 13:53:11 +01:00
it 'should redirect to barcode page' do
2015-09-07 14:26:07 +02:00
post :create, product: attributes_for(:product)
2015-09-26 16:26:58 +02:00
expect(response).to redirect_to action: :barcode
2015-09-07 14:26:07 +02:00
end
end
context 'failed' do
it 'should not create a product' do
expect{
post :create, product: attributes_for(:invalid_product)
2015-12-01 20:02:08 +01:00
}.to_not change{ Product.count}
2015-09-07 14:26:07 +02:00
end
it 'should render form' do
post :create, product: attributes_for(:invalid_product)
2015-09-26 16:26:58 +02:00
expect(response).to render_template(:link)
2015-09-07 14:26:07 +02:00
end
end
end
2015-09-22 07:43:31 +02:00
###########
# INDEX #
###########
2015-09-07 14:26:07 +02:00
describe 'GET index' do
it 'should load all the products' do
product = create :product
get :index
expect(assigns :products).to eq([product])
end
end
2015-09-22 07:43:31 +02:00
##########
# EDIT #
##########
2015-09-07 14:26:07 +02:00
describe 'GET edit' do
before :each do
@product = create :product
2015-10-29 13:53:11 +01:00
get :edit, id: @product
end
it 'should be successful' do
expect(response).to have_http_status(200)
2015-09-07 14:26:07 +02:00
end
it 'should render the correct form' do
expect(response).to render_template(:edit)
end
it 'should load the correct product' do
expect(assigns :product).to eq(@product)
end
end
2015-09-22 07:43:31 +02:00
############
# UPDATE #
############
2015-09-07 14:26:07 +02:00
describe 'PUT update' do
before :each do
@product = create :product
end
it 'loads right product' do
2015-10-29 13:53:11 +01:00
put :update, id: @product, product: attributes_for(:product)
2015-09-07 14:26:07 +02:00
expect(assigns :product).to eq(@product)
end
2015-09-22 07:43:31 +02:00
context 'successful' do
it 'should update attributes' do
put :update, id: @product, product: { name: "new_product_name" }
expect(@product.reload.name).to eq("new_product_name")
end
end
2015-09-07 14:26:07 +02:00
context 'failed' do
it 'should not update attributes' do
2015-10-29 13:53:11 +01:00
old_price = @product.price
2015-09-07 14:26:07 +02:00
put :update, id: @product, product: attributes_for(:invalid_product)
2015-10-29 13:53:11 +01:00
expect(@product.reload.price).to eq(old_price)
2015-09-07 14:26:07 +02:00
end
end
end
2015-09-22 07:43:31 +02:00
2015-10-29 13:53:11 +01:00
#############
# BARCODE #
#############
describe 'GET barcode' do
it 'should be successful' do
expect(response).to have_http_status(200)
end
end
2015-09-22 07:43:31 +02:00
##################
2015-10-29 13:53:11 +01:00
# LOAD_BARCODE #
2015-09-22 07:43:31 +02:00
##################
2015-10-29 13:53:11 +01:00
describe 'POST load_barcode' do
describe 'new barcode' do
before :each do
@code = attributes_for(:barcode)[:code].to_s
post :load_barcode, barcode: @code
end
it 'should be successful' do
expect(response).to have_http_status(200)
end
it 'should be render the link partial' do
expect(response).to render_template(:link)
end
it 'should allow building a new product' do
expect(assigns(:product).id).to be nil
end
it 'should have a barcode built for the new product' do
barcode = assigns(:product).barcodes.first
expect(barcode.code).to eq @code
end
end
describe 'existing barcode' do
before :each do
@barcode = create :barcode
post :load_barcode, barcode: @barcode.code
end
it 'should be successful' do
expect(response).to have_http_status(200)
end
it 'should be render the stock_entry partial' do
expect(response).to render_template(:stock_entry)
end
it 'should have the correct product loaded' do
expect(assigns :product).to eq @barcode.product
end
2015-09-22 07:43:31 +02:00
end
end
2015-09-07 14:26:07 +02:00
end