Skip to content

Commit

Permalink
Merge pull request #52 from zenith19/49099_rspec_course_model
Browse files Browse the repository at this point in the history
49099_rspec_course_model
  • Loading branch information
dieunb committed Dec 22, 2015
2 parents 9292d48 + c640a08 commit 06c6212
Show file tree
Hide file tree
Showing 9 changed files with 365 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ gem "htmlbeautifier"
group :development, :test do
# Call "byebug" anywhere in the code to stop execution and get a debugger console
gem "byebug"
gem "rspec-rails"
gem "factory_girl_rails"
gem "shoulda-matchers", "~> 2.5"
end

group :development do
Expand All @@ -57,3 +60,9 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem "spring"
end

group :test do
gem "capybara"
gem "guard-rspec"
gem "launchy"
end
81 changes: 81 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
addressable (2.4.0)
arel (6.0.3)
bcrypt (3.1.10)
binding_of_caller (0.7.2)
Expand All @@ -47,7 +48,14 @@ GEM
builder (3.2.2)
byebug (8.2.1)
cancancan (1.13.1)
capybara (2.5.0)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
chronic (0.10.2)
coderay (1.1.0)
coffee-rails (4.1.0)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
Expand All @@ -67,12 +75,34 @@ GEM
warden (~> 1.2.3)
devise-i18n (0.12.1)
devise-i18n-views (0.3.7)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.6.0)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
faker (1.4.2)
i18n (~> 0.5)
ffi (1.9.10)
formatador (0.2.5)
globalid (0.3.6)
activesupport (>= 4.1.0)
guard (2.13.0)
formatador (>= 0.2.4)
listen (>= 2.7, <= 4.0)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.1)
guard-rspec (4.6.4)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
htmlbeautifier (1.1.1)
i18n (0.7.0)
jbuilder (2.3.2)
Expand All @@ -83,22 +113,37 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
launchy (2.4.3)
addressable (~> 2.3)
listen (3.0.5)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
loofah (2.0.3)
nokogiri (>= 1.5.9)
lumberjack (1.0.9)
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.99)
mini_portile (0.6.2)
minitest (5.8.3)
multi_json (1.11.2)
mysql2 (0.4.2)
nenv (0.2.0)
nokogiri (1.6.6.4)
mini_portile (~> 0.6.0)
notiffany (0.0.8)
nenv (~> 0.1)
shellany (~> 0.0)
orm_adapter (0.5.0)
paranoia (2.1.4)
activerecord (~> 4.0)
polyamorous (1.3.0)
activerecord (>= 3.0)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
public_activity (1.4.2)
actionpack (>= 3.0.0)
activerecord (>= 3.0)
Expand Down Expand Up @@ -138,10 +183,34 @@ GEM
activesupport (>= 3.0)
i18n
polyamorous (~> 1.2)
rb-fsevent (0.9.6)
rb-inotify (0.9.5)
ffi (>= 0.5.0)
rdoc (4.2.0)
redis (3.2.2)
responders (2.1.0)
railties (>= 4.2.0, < 5)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-core (3.4.1)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-rails (3.4.0)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
sass (3.4.19)
sass-rails (5.0.4)
railties (>= 4.0.0, < 5.0)
Expand All @@ -152,11 +221,15 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
shellany (0.0.1)
shoulda-matchers (2.8.0)
activesupport (>= 3.0.0)
sidekiq (4.0.1)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
json (~> 1.0)
redis (~> 3.2, >= 3.2.1)
slop (3.6.0)
social-share-button (0.1.10)
coffee-rails
sass-rails
Expand Down Expand Up @@ -189,6 +262,8 @@ GEM
will_paginate (3.0.7)
will_paginate-bootstrap (1.0.1)
will_paginate (>= 3.0.3)
xpath (2.0.0)
nokogiri (~> 1.3)

PLATFORMS
ruby
Expand All @@ -198,21 +273,27 @@ DEPENDENCIES
bootstrap-sass (= 3.2.0.0)
byebug
cancancan
capybara
coffee-rails (~> 4.1.0)
devise
devise-i18n
devise-i18n-views
factory_girl_rails
faker (= 1.4.2)
guard-rspec
htmlbeautifier
jbuilder (~> 2.0)
jquery-rails
launchy
mysql2
paranoia (~> 2.0)
public_activity
rails (= 4.2.5)
ransack
rspec-rails
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
shoulda-matchers (~> 2.5)
sidekiq
social-share-button
spring
Expand Down
70 changes: 70 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

# Note: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
# * bundler: 'bundle exec rspec'
# * bundler binstubs: 'bin/rspec'
# * spring: 'bin/rspec' (This will use spring if running and you have
# installed the spring binstubs per the docs)
# * zeus: 'zeus rspec' (requires the server to be started separately)
# * 'just' rspec: 'rspec'

guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)

# Rails files
rails = dsl.rails(view_extensions: %w(erb haml slim))
dsl.watch_spec_files_for(rails.app_files)
dsl.watch_spec_files_for(rails.views)

watch(rails.controllers) do |m|
[
rspec.spec.("routing/#{m[1]}_routing"),
rspec.spec.("controllers/#{m[1]}_controller"),
rspec.spec.("acceptance/#{m[1]}")
]
end

# Rails config changes
watch(rails.spec_helper) { rspec.spec_dir }
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }

# Capybara features specs
watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
watch(rails.layouts) { |m| rspec.spec.("features/#{m[1]}") }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
end
end
8 changes: 5 additions & 3 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ def valid_for_status_update? has_status
end

def start_must_be_before_end_date
errors.add(:start_date, "must be before end date") unless
start_date < end_date
unless (start_date == nil || end_date == nil)
errors.add(:start_date, "must be before end date") unless
self.start_date < self.end_date
end
end

def default_values
self.assigned_users ||= []
self.removed_users ||= []
end
end
end
11 changes: 11 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,16 @@ class Application < Rails::Application

# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true

config.generators do |g|
g.test_framework :rspec,
fixtures: true,
view_specs: false,
helper_specs: false,
routing_specs: false,
controller_specs: true,
request_specs: true
g.fixture_replacement :factory_girl, dir: "spec/factories"
end
end
end
10 changes: 10 additions & 0 deletions spec/factories/courses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "faker"

FactoryGirl.define do
factory :course do |f|
f.name {Faker::Lorem.word}
f.description {Faker::Lorem.paragraph}
f.start_date {Faker::Date.backward 23}
f.end_date {Faker::Date.forward 23}
end
end
29 changes: 29 additions & 0 deletions spec/models/course_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "rails_helper"

describe Course do

it "has a valid factory" do
expect(FactoryGirl.create :course).to be_valid
end

it "is invalid without a name" do
expect(FactoryGirl.build(:course, name: nil)).not_to be_valid
end

it "is invalid without a description" do
expect(FactoryGirl.build(:course, description: nil)).not_to be_valid
end

it "is invalid without a start_date" do
expect(FactoryGirl.build(:course, start_date: nil)).not_to be_valid
end

it "is invalid without a end_date" do
expect(FactoryGirl.build(:course, end_date: nil)).not_to be_valid
end

it "has many lessons" do
t = Course.reflect_on_association :users
expect(t.macro).to eq :has_many
end
end
Loading

0 comments on commit 06c6212

Please sign in to comment.