From 084e542dd5a11bdd86db389d082047bc51ea6a09 Mon Sep 17 00:00:00 2001 From: benji Date: Mon, 7 Sep 2015 14:26:07 +0200 Subject: [PATCH] Add tests for products_controller --- app/models/user.rb | 1 - spec/controllers/products_controller_spec.rb | 97 ++++++++++++++++++++ spec/factories/products.rb | 4 + spec/factories/users.rb | 1 - spec/models/ability_spec.rb | 10 +- spec/models/order_item_spec.rb | 10 ++ spec/models/user_spec.rb | 1 - 7 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 spec/controllers/products_controller_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb index 91b3b25..f89e00a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,7 +3,6 @@ # Table name: users # # id :integer not null, primary key -# debt_cents :integer default("0"), not null # created_at :datetime # updated_at :datetime # remember_created_at :datetime diff --git a/spec/controllers/products_controller_spec.rb b/spec/controllers/products_controller_spec.rb new file mode 100644 index 0000000..c11da2f --- /dev/null +++ b/spec/controllers/products_controller_spec.rb @@ -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 diff --git a/spec/factories/products.rb b/spec/factories/products.rb index d971aa8..df8f592 100644 --- a/spec/factories/products.rb +++ b/spec/factories/products.rb @@ -27,5 +27,9 @@ FactoryGirl.define do stock { 30 + rand(30) } calories { rand 20 } avatar { Identicon.data_url_for name } + + factory :invalid_product do + name nil + end end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index e474960..0368c86 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -3,7 +3,6 @@ # Table name: users # # id :integer not null, primary key -# debt_cents :integer default("0"), not null # created_at :datetime # updated_at :datetime # remember_created_at :datetime diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index e417ef7..16cf917 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -1,11 +1,11 @@ -require "cancan/matchers" +require 'cancan/matchers' describe User do - describe "abilities" do + describe 'abilities' do subject(:ability){ Ability.new(user) } let(:user) { nil} - describe "as admin" do + describe 'as admin' do let(:user) { create :admin } it{ should be_able_to(:manage, Product.new) } @@ -14,7 +14,7 @@ describe User do it{ should be_able_to(:manage, User.new) } end - describe "as normal user" do + describe 'as normal user' do let(:user) { create :user } it{ should be_able_to(:read, Product.new) } @@ -29,7 +29,7 @@ describe User do it{ should_not be_able_to(:manage, User.new) } end - describe "as koelkast" do + describe 'as koelkast' do let(:user) { create :koelkast } it{ should_not be_able_to(:manage, Product.new) } diff --git a/spec/models/order_item_spec.rb b/spec/models/order_item_spec.rb index dbc8605..bdad4fe 100644 --- a/spec/models/order_item_spec.rb +++ b/spec/models/order_item_spec.rb @@ -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 it 'has a valid factory' do order_item = create :order_item diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3dfa9b2..2e57ba0 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -3,7 +3,6 @@ # Table name: users # # id :integer not null, primary key -# debt_cents :integer default("0"), not null # created_at :datetime # updated_at :datetime # remember_created_at :datetime