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
@current_ability ||=
current_client.try { |c| ClientAbility.new(c) } ||
Ability.new(current_user)
UserAbility.new(current_user)
end
end

View File

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

View File

@ -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,

View File

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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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