Add tests for products_controller
This commit is contained in:
parent
003e0bc88b
commit
084e542dd5
7 changed files with 116 additions and 8 deletions
|
@ -3,7 +3,6 @@
|
||||||
# Table name: users
|
# Table name: users
|
||||||
#
|
#
|
||||||
# id :integer not null, primary key
|
# id :integer not null, primary key
|
||||||
# debt_cents :integer default("0"), not null
|
|
||||||
# created_at :datetime
|
# created_at :datetime
|
||||||
# updated_at :datetime
|
# updated_at :datetime
|
||||||
# remember_created_at :datetime
|
# remember_created_at :datetime
|
||||||
|
|
97
spec/controllers/products_controller_spec.rb
Normal file
97
spec/controllers/products_controller_spec.rb
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
describe ProductsController, type: :controller do
|
||||||
|
before :each do
|
||||||
|
@admin = create :admin
|
||||||
|
sign_in @admin
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'POST create' do
|
||||||
|
context 'successfull' do
|
||||||
|
it 'should create a product' do
|
||||||
|
expect{
|
||||||
|
post :create, product: attributes_for(:product)
|
||||||
|
}.to change{Product.count}.by(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should redirect to index page' do
|
||||||
|
post :create, product: attributes_for(:product)
|
||||||
|
expect(response).to redirect_to action: :index
|
||||||
|
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)
|
||||||
|
expect(response).to render_template(:new)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET index' do
|
||||||
|
it 'should load all the products' do
|
||||||
|
product = create :product
|
||||||
|
get :index
|
||||||
|
expect(assigns :products).to eq([product])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
context 'successful' do
|
||||||
|
it 'should update attributes' do
|
||||||
|
attributes = attributes_for(:product)
|
||||||
|
attributes.merge(price: (attributes[:price_cents] / 100))
|
||||||
|
attributes.delete(:price_cents)
|
||||||
|
put :update, id: @product, product: attributes
|
||||||
|
new_attributes = @product.reload.attributes.symbolize_keys.slice(*attributes.keys)
|
||||||
|
expect(new_attributes).to eq(attributes.except(:avatar))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'failed' do
|
||||||
|
it 'should not update attributes' do
|
||||||
|
old_attributes = @product.attributes
|
||||||
|
put :update, id: @product, product: attributes_for(:invalid_product)
|
||||||
|
expect(@product.reload.attributes).to eq(old_attributes)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -27,5 +27,9 @@ FactoryGirl.define do
|
||||||
stock { 30 + rand(30) }
|
stock { 30 + rand(30) }
|
||||||
calories { rand 20 }
|
calories { rand 20 }
|
||||||
avatar { Identicon.data_url_for name }
|
avatar { Identicon.data_url_for name }
|
||||||
|
|
||||||
|
factory :invalid_product do
|
||||||
|
name nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
# Table name: users
|
# Table name: users
|
||||||
#
|
#
|
||||||
# id :integer not null, primary key
|
# id :integer not null, primary key
|
||||||
# debt_cents :integer default("0"), not null
|
|
||||||
# created_at :datetime
|
# created_at :datetime
|
||||||
# updated_at :datetime
|
# updated_at :datetime
|
||||||
# remember_created_at :datetime
|
# remember_created_at :datetime
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
require "cancan/matchers"
|
require 'cancan/matchers'
|
||||||
|
|
||||||
describe User do
|
describe User do
|
||||||
describe "abilities" do
|
describe 'abilities' do
|
||||||
subject(:ability){ Ability.new(user) }
|
subject(:ability){ Ability.new(user) }
|
||||||
let(:user) { nil}
|
let(:user) { nil}
|
||||||
|
|
||||||
describe "as admin" do
|
describe 'as admin' do
|
||||||
let(:user) { create :admin }
|
let(:user) { create :admin }
|
||||||
|
|
||||||
it{ should be_able_to(:manage, Product.new) }
|
it{ should be_able_to(:manage, Product.new) }
|
||||||
|
@ -14,7 +14,7 @@ describe User do
|
||||||
it{ should be_able_to(:manage, User.new) }
|
it{ should be_able_to(:manage, User.new) }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "as normal user" do
|
describe 'as normal user' do
|
||||||
let(:user) { create :user }
|
let(:user) { create :user }
|
||||||
|
|
||||||
it{ should be_able_to(:read, Product.new) }
|
it{ should be_able_to(:read, Product.new) }
|
||||||
|
@ -29,7 +29,7 @@ describe User do
|
||||||
it{ should_not be_able_to(:manage, User.new) }
|
it{ should_not be_able_to(:manage, User.new) }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "as koelkast" do
|
describe 'as koelkast' do
|
||||||
let(:user) { create :koelkast }
|
let(:user) { create :koelkast }
|
||||||
|
|
||||||
it{ should_not be_able_to(:manage, Product.new) }
|
it{ should_not be_able_to(:manage, Product.new) }
|
||||||
|
|
|
@ -1,3 +1,13 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: order_items
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# order_id :integer
|
||||||
|
# product_id :integer not null
|
||||||
|
# count :integer default("0")
|
||||||
|
#
|
||||||
|
|
||||||
describe OrderItem do
|
describe OrderItem do
|
||||||
it 'has a valid factory' do
|
it 'has a valid factory' do
|
||||||
order_item = create :order_item
|
order_item = create :order_item
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
# Table name: users
|
# Table name: users
|
||||||
#
|
#
|
||||||
# id :integer not null, primary key
|
# id :integer not null, primary key
|
||||||
# debt_cents :integer default("0"), not null
|
|
||||||
# created_at :datetime
|
# created_at :datetime
|
||||||
# updated_at :datetime
|
# updated_at :datetime
|
||||||
# remember_created_at :datetime
|
# remember_created_at :datetime
|
||||||
|
|
Loading…
Reference in a new issue