From 22d3e0c617ffadf94cbfeb9e840a6a9fe1494226 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 8 Sep 2015 14:00:11 +0200 Subject: [PATCH] Test caches --- spec/factories/clients.rb | 3 +-- spec/factories/transactions.rb | 4 ++-- spec/models/transaction_spec.rb | 21 ++++++++++++++++++++- spec/models/user_spec.rb | 4 +++- spec/spec_helper.rb | 2 ++ 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/spec/factories/clients.rb b/spec/factories/clients.rb index 03b1cda..5926219 100644 --- a/spec/factories/clients.rb +++ b/spec/factories/clients.rb @@ -11,7 +11,6 @@ FactoryGirl.define do factory :client do - name {{ Faker::Lorem.word }} + name { Faker::Lorem.word } end - end diff --git a/spec/factories/transactions.rb b/spec/factories/transactions.rb index 0419338..d0eea9f 100644 --- a/spec/factories/transactions.rb +++ b/spec/factories/transactions.rb @@ -14,8 +14,8 @@ FactoryGirl.define do factory :transaction do - debtor - creditor + association :debtor, factory: :user + association :creditor, factory: :user amount { rand(100) } origin 'FactoryGirl' message { Faker::Lorem.sentence } diff --git a/spec/models/transaction_spec.rb b/spec/models/transaction_spec.rb index 07cf2b7..805bf63 100644 --- a/spec/models/transaction_spec.rb +++ b/spec/models/transaction_spec.rb @@ -15,5 +15,24 @@ require 'rails_helper' RSpec.describe Transaction, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + it "has a valid factory" do + expect(create(:transaction)).to be_valid + end + + describe "cache" do + before :each do + @user = create(:user) + end + + it "should update creditor cache" do + trans = build(:transaction, creditor: @user, amount: 10) + expect {trans.save!}.to change {@user.balance}.by(10) + end + + it "should update debtor cache" do + trans = build(:transaction, debtor: @user, amount: 10) + expect {trans.save!}.to change {@user.balance}.by(-10) + end + end + end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 4a84ec7..cc98894 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -13,5 +13,7 @@ require 'rails_helper' RSpec.describe User, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + it "has a valid factory" do + expect(create(:user)).to be_valid + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 913e28a..87ddb8c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,7 +16,9 @@ # users commonly want. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +require 'factory_girl' RSpec.configure do |config| + config.include FactoryGirl::Syntax::Methods # rspec-expectations config goes here. You can use an alternate # assertion/expectation library such as wrong or the stdlib/minitest # assertions if you prefer.