Fix tests
This commit is contained in:
parent
bd27ac3cca
commit
f35f8bec41
5 changed files with 48 additions and 15 deletions
|
@ -45,7 +45,7 @@ describe ProductsController, type: :controller do
|
|||
|
||||
it 'should redirect to index page' do
|
||||
post :create, product: attributes_for(:product)
|
||||
expect(response).to redirect_to action: :index
|
||||
expect(response).to redirect_to action: :barcode
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -58,7 +58,7 @@ describe ProductsController, type: :controller do
|
|||
|
||||
it 'should render form' do
|
||||
post :create, product: attributes_for(:invalid_product)
|
||||
expect(response).to render_template(:new)
|
||||
expect(response).to render_template(:link)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -133,7 +133,8 @@ describe ProductsController, type: :controller do
|
|||
describe 'POST from_barcode' do
|
||||
it 'should return a product when barcode in database' do
|
||||
product = create :product
|
||||
post :from_barcode, barcode: product.barcode
|
||||
bar = create :barcode, product: product
|
||||
post :from_barcode, barcode: bar.code
|
||||
expect(JSON.parse(response.body)["id"]).to eq(product.id)
|
||||
end
|
||||
end
|
||||
|
|
17
spec/factories/barcodes.rb
Normal file
17
spec/factories/barcodes.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: barcodes
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# product_id :integer
|
||||
# code :string default(""), not null
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
#
|
||||
|
||||
FactoryGirl.define do
|
||||
factory :barcode do
|
||||
product
|
||||
sequence :code
|
||||
end
|
||||
end
|
|
@ -28,7 +28,6 @@ FactoryGirl.define do
|
|||
stock { 30 + rand(30) }
|
||||
calories { rand 20 }
|
||||
avatar { Identicon.data_url_for name }
|
||||
sequence :barcode
|
||||
|
||||
factory :invalid_product do
|
||||
name nil
|
||||
|
|
27
spec/models/barcode_spec.rb
Normal file
27
spec/models/barcode_spec.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
describe Barcode do
|
||||
before :each do
|
||||
@barcode = create :barcode
|
||||
end
|
||||
|
||||
it 'has a valid factory' do
|
||||
expect(@barcode).to be_valid
|
||||
end
|
||||
|
||||
############
|
||||
# FIELDS #
|
||||
############
|
||||
|
||||
describe 'fields' do
|
||||
describe 'code' do
|
||||
it 'should be present' do
|
||||
@barcode.code = nil
|
||||
expect(@barcode).to_not be_valid
|
||||
end
|
||||
|
||||
it 'should be unique' do
|
||||
barcode = build :barcode, code: @barcode.code
|
||||
expect(barcode).to_not be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -87,17 +87,6 @@ describe Product do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'barcode' do
|
||||
it 'should be present' do
|
||||
@product.barcode = nil
|
||||
expect(@product).to_not be_valid
|
||||
end
|
||||
|
||||
it 'should be unique' do
|
||||
expect(build :product, barcode: @product.barcode).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe 'avatar' do
|
||||
it 'should be present' do
|
||||
@product.avatar = nil
|
||||
|
|
Loading…
Reference in a new issue