Skip to content
This repository was archived by the owner on May 29, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do

gem 'rspec-rails', '~> 3.0'

gem 'shoulda-matchers', require: false
gem 'factory_girl_rails'

# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
shoulda-matchers (2.8.0)
activesupport (>= 3.0.0)
spring (1.3.6)
sprockets (3.2.0)
rack (~> 1.0)
Expand Down Expand Up @@ -177,6 +179,7 @@ DEPENDENCIES
rspec-rails (~> 3.0)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
shoulda-matchers
spring
sqlite3
turbolinks
Expand Down
2 changes: 1 addition & 1 deletion app/models/learning_material.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class LearningMaterial < ActiveRecord::Base
validates :topic, :source,
presence: true

default_scope { order('created_at DESC') }
scope :ordered, -> { order('created_at DESC') }
end
3 changes: 3 additions & 0 deletions app/models/lesson.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class Lesson < ActiveRecord::Base
has_many :learning_materials, dependent: :destroy

validates :title, presence: true

end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@

en:
hello: "Hello world"
topic: "can't be blank"
33 changes: 0 additions & 33 deletions spec/controllers/lessons_controller_spec.rb

This file was deleted.

9 changes: 9 additions & 0 deletions spec/factories/learning_materials.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryGirl.define do
factory :learning_material do
topic 'Funny Lesson'
source 'Book in the closet'
description 'Very funny lesson on a great subject'
level 'Basic'
association :lesson, factory: :lesson
end
end
8 changes: 4 additions & 4 deletions spec/factories/lessons.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FactoryGirl.define do
factory :lesson do
title "MyString"
chapter "MyString"
category "MyString"
goal "MyText"
title 'Intro'
chapter '1 Intro'
category 'Rails'
goal 'First intro, your first web app'
end
end
51 changes: 49 additions & 2 deletions spec/models/learning_material_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
require 'rails_helper'

RSpec.describe LearningMaterial, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
describe LearningMaterial, type: :model do

context 'with valid data' do

it 'has a valid factory' do
expect(build(:learning_material)).to be_valid
end

it 'is valid with validated fields filled in' do
learning_material = build(:learning_material, topic: 'Mooie titel', source: 'Ruby Nuby Book')
expect(learning_material).to be_valid
end

it 'is valid with blank non-validated fields' do
learning_material = build(:learning_material, description: nil, level: nil)
expect(learning_material).to be_valid
end

it { should belong_to(:lesson) }

context 'with scope' do
#not for daily behaviour, but f.e. when changing the dbase etc
it 'should return the materials in the correct order' do
@last = create(:learning_material, created_at: 1.day.ago)
@first = create(:learning_material, created_at: 3.day.ago)
expect(LearningMaterial.ordered.to_a).to eq([@last, @first])
end
end

end


context 'with invalid data' do

it 'is invalid without a topic' do
learning_material = build(:learning_material, topic: nil)
expect(learning_material).not_to be_valid
expect(learning_material.errors[:topic]).to include("can't be blank")
end

it 'is invalid without a source' do
learning_material = build(:learning_material, source: nil)
expect(learning_material).not_to be_valid
expect(learning_material.errors[:source]).to include("can't be blank")
end
end

end


38 changes: 35 additions & 3 deletions spec/models/lesson_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
require 'rails_helper'

RSpec.describe Lesson, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
describe Lesson, type: :model do


context 'with valid data' do

it 'has a valid factory' do
expect(build(:lesson)).to be_valid
end

it 'is valid with validated fields filled in' do
lesson= build(:lesson, title: 'Mooie titel')
expect(lesson).to be_valid
end

it 'is valid with blank non-validated fields' do
lesson = build(:lesson, chapter: nil, category: nil, goal: nil)
expect(lesson).to be_valid
end

it { should have_many(:learning_materials) }
it { should have_many(:learning_materials).dependent(:destroy) }

end


context 'with invalid data' do

it 'is invalid without a title' do
lesson = build(:lesson, title: nil)
expect(lesson).not_to be_valid
expect(lesson.errors[:title]).to include("can't be blank")
end
end

end
13 changes: 12 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'shoulda/matchers'
require 'factory_girl'
# Add additional requires below this line. Rails is not loaded until this point!

Expand All @@ -29,8 +30,18 @@

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
#config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include FactoryGirl::Syntax::Methods
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end

config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
Expand Down