From 4c44b3f7384a0f6f135f5f5f61bf1f88d5227046 Mon Sep 17 00:00:00 2001 From: benji Date: Wed, 9 Sep 2015 20:11:59 +0200 Subject: [PATCH] Rspec tests --- app/controllers/application_controller.rb | 2 +- app/models/client_ability.rb | 2 +- app/models/user.rb | 1 + app/models/{ability.rb => user_ability.rb} | 2 +- .../transactions_controller_spec.rb | 6 ++-- spec/controllers/users_controller_spec.rb | 33 +++++++++++++++++++ spec/factories/transactions.rb | 2 +- spec/models/client_spec.rb | 12 +++++-- spec/models/transaction_spec.rb | 16 +++++++++ spec/models/user_spec.rb | 11 ++++++- 10 files changed, 77 insertions(+), 10 deletions(-) rename app/models/{ability.rb => user_ability.rb} (91%) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c7cc410..0b697fd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,6 +18,6 @@ class ApplicationController < ActionController::Base def current_ability @current_ability ||= current_client.try { |c| ClientAbility.new(c) } || - Ability.new(current_user) + UserAbility.new(current_user) end end diff --git a/app/models/client_ability.rb b/app/models/client_ability.rb index 1f804eb..1a5dd66 100644 --- a/app/models/client_ability.rb +++ b/app/models/client_ability.rb @@ -1,4 +1,4 @@ -class Ability +class ClientAbility include CanCan::Ability def initialize(client) diff --git a/app/models/user.rb b/app/models/user.rb index c5886cf..6e98824 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,6 +12,7 @@ class User < ActiveRecord::Base devise :timeoutable, :omniauthable, :omniauth_providers => [:zeuswpi] + has_many :incoming_transactions, class_name: 'Transaction', foreign_key: 'creditor_id' has_many :outgoing_transactions, diff --git a/app/models/ability.rb b/app/models/user_ability.rb similarity index 91% rename from app/models/ability.rb rename to app/models/user_ability.rb index c3a4c81..47af0c8 100644 --- a/app/models/ability.rb +++ b/app/models/user_ability.rb @@ -1,4 +1,4 @@ -class Ability +class UserAbility include CanCan::Ability def initialize(user) diff --git a/spec/controllers/transactions_controller_spec.rb b/spec/controllers/transactions_controller_spec.rb index e96cc51..4bd2e9d 100644 --- a/spec/controllers/transactions_controller_spec.rb +++ b/spec/controllers/transactions_controller_spec.rb @@ -22,7 +22,7 @@ RSpec.describe TransactionsController, type: :controller do end it "should create a new transaction" do - expect {post :create, @attributes}.to change {Transaction.count}.by(1) + expect { post :create, @attributes }.to change { Transaction.count }.by(1) end it "should set debtor" do @@ -58,7 +58,7 @@ RSpec.describe TransactionsController, type: :controller do it "should be refused" do expect do post :create, transaction: attributes_for(:transaction, cents: -20) - end.not_to change {Transaction.count} + end.not_to change { Transaction.count } end end @@ -71,7 +71,7 @@ RSpec.describe TransactionsController, type: :controller do euros: 10000000, message: 'DIT IS OVERVAL' } - end.not_to change {Transaction.count} + end.not_to change { Transaction.count } end end end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index e2c3d3b..f681ffe 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -1,5 +1,38 @@ require 'rails_helper' RSpec.describe UsersController, type: :controller do + before :each do + @user = create :penning + sign_in @user + end + describe 'GET show' do + before :each do + get :show, id: @user + end + + it 'should be successful' do + expect(response).to render_template(:show) + expect(response).to have_http_status(200) + end + + it 'should load the correct user' do + expect(assigns(:user)).to eq(@user) + end + end + + describe 'GET index' do + before :each do + get :index + end + + it 'should load an array of all users' do + expect(assigns(:users)).to eq([@user]) + end + + it 'should render the correct template' do + expect(response).to have_http_status(200) + expect(response).to render_template(:index) + end + end end diff --git a/spec/factories/transactions.rb b/spec/factories/transactions.rb index 716b419..71e7e00 100644 --- a/spec/factories/transactions.rb +++ b/spec/factories/transactions.rb @@ -18,7 +18,7 @@ FactoryGirl.define do association :debtor, factory: :user association :creditor, factory: :user issuer { debtor } - amount { rand(100) } + amount { 1 + rand(100) } message { Faker::Lorem.sentence } end end diff --git a/spec/models/client_spec.rb b/spec/models/client_spec.rb index 33e905f..3b23675 100644 --- a/spec/models/client_spec.rb +++ b/spec/models/client_spec.rb @@ -12,11 +12,19 @@ require 'rails_helper' RSpec.describe Client, type: :model do + before :each do + @client = create :client + end it "should have a valid factory" do - expect(create(:client)).to be_valid + expect(@client).to be_valid end it "should generate a key" do - expect(create(:client).key).to be_present + expect(@client.key).to be_present + end + + it "should have a unique name" do + client = build :client, name: @client.name + expect(client).to_not be_valid end end diff --git a/spec/models/transaction_spec.rb b/spec/models/transaction_spec.rb index d05aaf8..1a9ab8c 100644 --- a/spec/models/transaction_spec.rb +++ b/spec/models/transaction_spec.rb @@ -36,4 +36,20 @@ RSpec.describe Transaction, type: :model do end end + describe "amount" do + it "should be positive" do + expect(build :transaction, amount: -5).to_not be_valid + end + + it "should not be 0" do + expect(build :transaction, amount: 0).to_not be_valid + end + end + + describe "debtor/creditor" do + it "should be different" do + @user = create :user + expect(build :transaction, debtor: @user, creditor: @user).to_not be_valid + end + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index cc98894..812c023 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -13,7 +13,16 @@ require 'rails_helper' RSpec.describe User, type: :model do + before :each do + @user = create :user + end + it "has a valid factory" do - expect(create(:user)).to be_valid + expect(@user).to be_valid + end + + it "has a unique name" do + user = build :user, name: @user.name + expect(user).to_not be_valid end end