tap/spec/controllers/products_controller_spec.rb

142 lines
3.7 KiB
Ruby
Raw Normal View History

2015-09-22 07:43:31 +02:00
# from_barcode_products POST /products/barcode(.:format) products#from_barcode
# products GET /products(.:format) products#index
# POST /products(.:format) products#create
# new_product GET /products/new(.:format) products#new
# edit_product GET /products/:id/edit(.:format) products#edit
# product PATCH /products/:id(.:format) products#update
# PUT /products/:id(.:format) products#update
#
2015-09-07 14:26:07 +02:00
describe ProductsController, type: :controller do
before :each do
@admin = create :admin
sign_in @admin
end
2015-09-22 07:43:31 +02:00
#########
# NEW #
#########
2015-09-07 14:26:07 +02:00
describe 'GET new' do
it 'should render the form' do
get :new
expect(response).to render_template(:new)
expect(response).to have_http_status(200)
end
2015-09-22 07:43:31 +02:00
it 'should initialize a new product' do
get :new
expect(assigns(:product).class).to eq(Product)
expect(assigns(:product)).to_not be_persisted
end
2015-09-07 14:26:07 +02:00
end
2015-09-22 07:43:31 +02:00
##########
# POST #
##########
2015-09-07 14:26:07 +02:00
describe 'POST create' do
context 'successfull' do
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
it 'should redirect to index page' do
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)
}.to_not change{Product.count}
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
end
it 'should render the correct form' do
get :edit, id: @product
expect(response).to render_template(:edit)
expect(response).to have_http_status(200)
end
it 'should load the correct product' do
get :edit, id: @product
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
put :edit, id: @product, product: attributes_for(:product)
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-09-22 07:43:31 +02:00
old_attributes = @product.attributes
2015-09-07 14:26:07 +02:00
put :update, id: @product, product: attributes_for(:invalid_product)
expect(@product.reload.attributes).to eq(old_attributes)
end
end
end
2015-09-22 07:43:31 +02:00
##################
# FROM_BARCODE #
##################
describe 'POST from_barcode' do
it 'should return a product when barcode in database' do
product = create :product
2015-09-26 16:26:58 +02:00
bar = create :barcode, product: product
post :from_barcode, barcode: bar.code
2015-09-22 07:43:31 +02:00
expect(JSON.parse(response.body)["id"]).to eq(product.id)
end
end
2015-09-07 14:26:07 +02:00
end