Skip to content

Commit dc463a3

Browse files
committed
start adding some rspec tests
1 parent c18f285 commit dc463a3

File tree

14 files changed

+393
-2
lines changed

14 files changed

+393
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@
2323
# bower files
2424
vendor/assets/bower_components
2525
database.yml
26+
.rspec

Gemfile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@ gem 'bootstrap-kaminari-views'
4949
# backends for mint api
5050
gem 'state_machine'
5151
gem 'chronic'
52-
gem 'selenium-webdriver'
52+
gem 'selenium-webdriver'
53+
54+
group :test do
55+
gem 'rspec-rails'
56+
gem 'factory_girl_rails'
57+
gem 'database_cleaner'
58+
end
59+
60+
group :development do
61+
gem 'pry'
62+
gem 'pry-remote'
63+
end

Gemfile.lock

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,23 @@ GEM
3939
childprocess (0.5.5)
4040
ffi (~> 1.0, >= 1.0.11)
4141
chronic (0.10.2)
42+
coderay (1.1.0)
4243
coffee-rails (4.0.1)
4344
coffee-script (>= 2.2.0)
4445
railties (>= 4.0.0, < 5.0)
4546
coffee-script (2.3.0)
4647
coffee-script-source
4748
execjs
4849
coffee-script-source (1.8.0)
50+
database_cleaner (1.3.0)
51+
diff-lcs (1.2.5)
4952
erubis (2.7.0)
5053
execjs (2.2.1)
54+
factory_girl (4.4.0)
55+
activesupport (>= 3.0.0)
56+
factory_girl_rails (4.4.1)
57+
factory_girl (~> 4.4.0)
58+
railties (>= 3.0.0)
5159
ffi (1.9.5)
5260
figaro (1.0.0)
5361
thor (~> 0.14)
@@ -64,12 +72,20 @@ GEM
6472
activesupport (>= 3.0.0)
6573
mail (2.6.1)
6674
mime-types (>= 1.16, < 3)
75+
method_source (0.8.2)
6776
mime-types (2.3)
6877
minitest (5.4.2)
6978
multi_json (1.10.1)
7079
pg (0.17.1)
7180
polyamorous (1.1.0)
7281
activerecord (>= 3.0)
82+
pry (0.10.1)
83+
coderay (~> 1.1.0)
84+
method_source (~> 0.8.1)
85+
slop (~> 3.4)
86+
pry-remote (0.1.8)
87+
pry (~> 0.9)
88+
slop (~> 3.0)
7389
rack (1.5.2)
7490
rack-test (0.6.2)
7591
rack (>= 1.0)
@@ -95,6 +111,22 @@ GEM
95111
activesupport (>= 3.0)
96112
i18n
97113
polyamorous (~> 1.1)
114+
rspec-core (3.1.5)
115+
rspec-support (~> 3.1.0)
116+
rspec-expectations (3.1.2)
117+
diff-lcs (>= 1.2.0, < 2.0)
118+
rspec-support (~> 3.1.0)
119+
rspec-mocks (3.1.2)
120+
rspec-support (~> 3.1.0)
121+
rspec-rails (3.1.0)
122+
actionpack (>= 3.0)
123+
activesupport (>= 3.0)
124+
railties (>= 3.0)
125+
rspec-core (~> 3.1.0)
126+
rspec-expectations (~> 3.1.0)
127+
rspec-mocks (~> 3.1.0)
128+
rspec-support (~> 3.1.0)
129+
rspec-support (3.1.1)
98130
rubyzip (1.1.6)
99131
sass (3.2.19)
100132
sass-rails (4.0.3)
@@ -107,6 +139,7 @@ GEM
107139
multi_json (~> 1.0)
108140
rubyzip (~> 1.0)
109141
websocket (~> 1.0)
142+
slop (3.6.0)
110143
sprockets (2.11.0)
111144
hike (~> 1.2)
112145
multi_json (~> 1.0)
@@ -145,13 +178,18 @@ DEPENDENCIES
145178
bower-rails
146179
chronic
147180
coffee-rails
181+
database_cleaner
182+
factory_girl_rails
148183
figaro
149184
haml
150185
jquery-rails
151186
kaminari
152187
pg
188+
pry
189+
pry-remote
153190
rails (= 4.1.6)
154191
ransack
192+
rspec-rails
155193
sass-rails (~> 4.0.3)
156194
selenium-webdriver
157195
state_machine

app/models/category.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class Category < ActiveRecord::Base
55

66
ALLOWED_ROOTS = %w(Income Expense Transfers).freeze
77

8-
# TODO: fixturize this data
98
def self.income
109
@income ||= roots.where(name: "Income").first
1110
end
@@ -26,11 +25,21 @@ def self.transfer_to
2625
@transfer_to ||= where(parent_id: transfers, name: "Transfer To").first
2726
end
2827

28+
def self.invalidate_cache!
29+
@income = nil
30+
@expense = nil
31+
@transfers = nil
32+
@transfer_from = nil
33+
@transfer_to = nil
34+
end
35+
2936
scope :budgeted, -> { where("budgeted_cents > 0") }
3037
scope :unbudgeted, -> { where("budgeted_cents is null or budgeted_cents <= 0") }
3138

3239
validates_inclusion_of :name, in: ALLOWED_ROOTS, if: :root?
3340

41+
after_create { Category.invalidate_cache! }
42+
3443
def can_edit?
3544
can_destroy?
3645
end

db/seeds.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@
55
#
66
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
77
# Mayor.create(name: 'Emanuel', city: cities.first)
8+
9+
income = Category.find_or_create_by(name: "Income")
10+
expense = Category.find_or_create_by(name: "Expense")
11+
transfers = Category.find_or_create_by(name: "Transfers")
12+
13+
Category.find_or_create_by(name: "Transfer From", parent_id: transfers.id)
14+
Category.find_or_create_by(name: "Transfer To", parent_id: transfers.id)

spec/factories/accounts.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
FactoryGirl.define do
3+
4+
factory :account do
5+
name "MyAccount"
6+
end
7+
8+
end

spec/factories/category.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
FactoryGirl.define do
3+
4+
factory :category do
5+
parent_id { Category.expense.id }
6+
name "Restaurant"
7+
end
8+
9+
factory :expense_category, parent: :category do
10+
end
11+
12+
factory :income_category, parent: :category do
13+
parent_id { Category.income.id }
14+
end
15+
16+
end

spec/factories/transactions.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
FactoryGirl.define do
3+
4+
factory :transaction do
5+
account
6+
category
7+
cents 100
8+
date { Date.today }
9+
description "Tim Hortons"
10+
end
11+
12+
end

spec/models/account_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'rails_helper'
2+
3+
describe Account do
4+
# nothing to do yet
5+
end

spec/models/category_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'rails_helper'
2+
3+
describe Category do
4+
5+
describe "validations" do
6+
it "can only be Income, Expense or Transfer when it is a root category" do
7+
expect(build(:category, parent: nil, name: "Foo")).not_to be_valid
8+
end
9+
end
10+
11+
end

spec/models/transaction_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'rails_helper'
2+
3+
describe Transaction do
4+
5+
describe "validation" do
6+
it "requires an account" do
7+
expect(build(:transaction, account: nil)).not_to be_valid
8+
end
9+
10+
it "requires a category" do
11+
expect(build(:transaction, category: nil)).not_to be_valid
12+
end
13+
14+
it "requires the amount" do
15+
expect(build(:transaction, cents: nil)).not_to be_valid
16+
end
17+
18+
it "requires the date" do
19+
expect(build(:transaction, date: nil)).not_to be_valid
20+
end
21+
22+
it "requires a description" do
23+
expect(build(:transaction, description: nil)).not_to be_valid
24+
end
25+
end
26+
27+
end

spec/rails_helper.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This file is copied to spec/ when you run 'rails generate rspec:install'
2+
ENV["RAILS_ENV"] ||= 'test'
3+
require 'spec_helper'
4+
require File.expand_path("../../config/environment", __FILE__)
5+
require 'rspec/rails'
6+
require Rails.root.join("db", "seeds")
7+
8+
require 'pry'
9+
10+
# Add additional requires below this line. Rails is not loaded until this point!
11+
12+
# Requires supporting ruby files with custom matchers and macros, etc, in
13+
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
14+
# run as spec files by default. This means that files in spec/support that end
15+
# in _spec.rb will both be required and run as specs, causing the specs to be
16+
# run twice. It is recommended that you do not name files matching this glob to
17+
# end with _spec.rb. You can configure this pattern with the --pattern
18+
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
19+
#
20+
# The following line is provided for convenience purposes. It has the downside
21+
# of increasing the boot-up time by auto-requiring all files in the support
22+
# directory. Alternatively, in the individual `*_spec.rb` files, manually
23+
# require only the support files necessary.
24+
#
25+
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
26+
27+
# Checks for pending migrations before tests are run.
28+
# If you are not using ActiveRecord, you can remove this line.
29+
ActiveRecord::Migration.maintain_test_schema!
30+
31+
RSpec.configure do |config|
32+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
33+
# examples within a transaction, remove the following line or assign false
34+
# instead of true.
35+
config.use_transactional_fixtures = true
36+
37+
# RSpec Rails can automatically mix in different behaviours to your tests
38+
# based on their file location, for example enabling you to call `get` and
39+
# `post` in specs under `spec/controllers`.
40+
#
41+
# You can disable this behaviour by removing the line below, and instead
42+
# explicitly tag your specs with their type, e.g.:
43+
#
44+
# RSpec.describe UsersController, :type => :controller do
45+
# # ...
46+
# end
47+
#
48+
# The different available types are documented in the features, such as in
49+
# https://relishapp.com/rspec/rspec-rails/docs
50+
config.infer_spec_type_from_file_location!
51+
52+
# Include FactoryGirl helpers
53+
config.include FactoryGirl::Syntax::Methods
54+
55+
# Before running the suite build all factory objects and ensure they are #valid?
56+
config.before(:suite) do
57+
FactoryGirl.lint
58+
end
59+
60+
config.before(:each) do
61+
DatabaseCleaner.strategy = :transaction
62+
end
63+
64+
config.after(:each) do
65+
DatabaseCleaner.clean
66+
end
67+
end

0 commit comments

Comments
 (0)