Rspec tests

This commit is contained in:
benji 2015-09-09 20:11:59 +02:00
parent 86b751fb81
commit 4c44b3f738
10 changed files with 77 additions and 10 deletions

View file

@ -18,6 +18,6 @@ class ApplicationController < ActionController::Base
def current_ability def current_ability
@current_ability ||= @current_ability ||=
current_client.try { |c| ClientAbility.new(c) } || current_client.try { |c| ClientAbility.new(c) } ||
Ability.new(current_user) UserAbility.new(current_user)
end end
end end

View file

@ -1,4 +1,4 @@
class Ability class ClientAbility
include CanCan::Ability include CanCan::Ability
def initialize(client) def initialize(client)

View file

@ -12,6 +12,7 @@
class User < ActiveRecord::Base class User < ActiveRecord::Base
devise :timeoutable, :omniauthable, :omniauth_providers => [:zeuswpi] devise :timeoutable, :omniauthable, :omniauth_providers => [:zeuswpi]
has_many :incoming_transactions, has_many :incoming_transactions,
class_name: 'Transaction', foreign_key: 'creditor_id' class_name: 'Transaction', foreign_key: 'creditor_id'
has_many :outgoing_transactions, has_many :outgoing_transactions,

View file

@ -1,4 +1,4 @@
class Ability class UserAbility
include CanCan::Ability include CanCan::Ability
def initialize(user) def initialize(user)

View file

@ -22,7 +22,7 @@ RSpec.describe TransactionsController, type: :controller do
end end
it "should create a new transaction" do 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 end
it "should set debtor" do it "should set debtor" do
@ -58,7 +58,7 @@ RSpec.describe TransactionsController, type: :controller do
it "should be refused" do it "should be refused" do
expect do expect do
post :create, transaction: attributes_for(:transaction, cents: -20) post :create, transaction: attributes_for(:transaction, cents: -20)
end.not_to change {Transaction.count} end.not_to change { Transaction.count }
end end
end end
@ -71,7 +71,7 @@ RSpec.describe TransactionsController, type: :controller do
euros: 10000000, euros: 10000000,
message: 'DIT IS OVERVAL' message: 'DIT IS OVERVAL'
} }
end.not_to change {Transaction.count} end.not_to change { Transaction.count }
end end
end end
end end

View file

@ -1,5 +1,38 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe UsersController, type: :controller do 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 end

View file

@ -18,7 +18,7 @@ FactoryGirl.define do
association :debtor, factory: :user association :debtor, factory: :user
association :creditor, factory: :user association :creditor, factory: :user
issuer { debtor } issuer { debtor }
amount { rand(100) } amount { 1 + rand(100) }
message { Faker::Lorem.sentence } message { Faker::Lorem.sentence }
end end
end end

View file

@ -12,11 +12,19 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Client, type: :model do RSpec.describe Client, type: :model do
before :each do
@client = create :client
end
it "should have a valid factory" do it "should have a valid factory" do
expect(create(:client)).to be_valid expect(@client).to be_valid
end end
it "should generate a key" do 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
end end

View file

@ -36,4 +36,20 @@ RSpec.describe Transaction, type: :model do
end end
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 end

View file

@ -13,7 +13,16 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe User, type: :model do RSpec.describe User, type: :model do
before :each do
@user = create :user
end
it "has a valid factory" do 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
end end