Add tests for barcode controller

This commit is contained in:
benji 2015-12-01 20:02:08 +01:00
parent e3fe271c4f
commit d361abc91e
3 changed files with 81 additions and 5 deletions

View file

@ -0,0 +1,72 @@
# product_barcodes POST /products/:product_id/barcodes(.:format) barcodes#create
# barcodes GET /barcodes(.:format) barcodes#index
# barcode GET /barcodes/:id(.:format) barcodes#show
# DELETE /barcodes/:id(.:format) barcodes#destroy
#
describe BarcodesController, type: :controller do
before :each do
@product = create :product
@admin = create :admin
sign_in @admin
end
##########
# POST #
##########
describe 'POST create' do
context 'successful' do
it 'should create a barcode' do
expect{
post :create, product_id: @product, barcode: attributes_for(:barcode)
}.to change{ Barcode.count }.by(1)
end
end
context 'failed' do
it 'should not create a barcode' do
expect{
post :create, product_id: @product, barcode: attributes_for(:invalid_barcode)
}.to_not change{ Barcode.count }
end
end
end
###########
# INDEX #
###########
describe 'GET index' do
it 'should load all the barcodes' do
barcode = create :barcode
get :index
expect(assigns(:barcodes)).to eq([barcode])
end
end
##########
# SHOW #
##########
describe 'GET show' do
before :each do
@barcode = create :barcode
end
it 'should load the correct barcode' do
get :show, id: @barcode
expect(assigns(:barcode)).to eq(@barcode)
end
it 'should allow friendly id' do
get :show, id: @barcode.code
expect(assigns(:barcode)).to eq(@barcode)
end
it 'should respond with this barcode' do
get :show, id: @barcode
expect(response.body).to eq @barcode.product.to_json
end
end
end

View file

@ -13,12 +13,12 @@ describe ProductsController, type: :controller do
sign_in @admin
end
##########
# POST #
##########
############
# CREATE #
############
describe 'POST create' do
context 'successfull' do
context 'successful' do
it 'should create a product' do
expect{
post :create, product: attributes_for(:product)
@ -35,7 +35,7 @@ describe ProductsController, type: :controller do
it 'should not create a product' do
expect{
post :create, product: attributes_for(:invalid_product)
}.to_not change{Product.count}
}.to_not change{ Product.count}
end
it 'should render form' do

View file

@ -13,5 +13,9 @@ FactoryGirl.define do
factory :barcode do
product
sequence :code
factory :invalid_barcode do
code nil
end
end
end