From e98760a552d0715a9f716ca54b0cc7fedfe9ad0d Mon Sep 17 00:00:00 2001 From: F3PiX Date: Wed, 19 Aug 2015 21:49:18 +0200 Subject: [PATCH 1/9] Factory --- spec/factories/lessons.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/factories/lessons.rb b/spec/factories/lessons.rb index cdea723..8d88072 100644 --- a/spec/factories/lessons.rb +++ b/spec/factories/lessons.rb @@ -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 webapp" end end From 2bd9414be7a13d9e120dc960862415beecb4fa9c Mon Sep 17 00:00:00 2001 From: F3PiX Date: Wed, 19 Aug 2015 22:15:40 +0200 Subject: [PATCH 2/9] Specs and validation Lesson model --- app/models/lesson.rb | 3 +++ spec/factories/lessons.rb | 8 ++++---- spec/models/lesson_spec.rb | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/app/models/lesson.rb b/app/models/lesson.rb index a2bd263..714ee2c 100644 --- a/app/models/lesson.rb +++ b/app/models/lesson.rb @@ -1,3 +1,6 @@ class Lesson < ActiveRecord::Base has_many :learning_materials, dependent: :destroy + + validates :title, presence: true + end diff --git a/spec/factories/lessons.rb b/spec/factories/lessons.rb index 8d88072..27d032c 100644 --- a/spec/factories/lessons.rb +++ b/spec/factories/lessons.rb @@ -1,8 +1,8 @@ FactoryGirl.define do factory :lesson do - title "Intro" - chapter "1 Intro" - category "Rails" - goal "First intro, your first webapp" + title 'Intro' + chapter '1 Intro' + category 'Rails' + goal 'First intro, your first web app' end end diff --git a/spec/models/lesson_spec.rb b/spec/models/lesson_spec.rb index 2a54341..fd00863 100644 --- a/spec/models/lesson_spec.rb +++ b/spec/models/lesson_spec.rb @@ -1,5 +1,33 @@ 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 + 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 \ No newline at end of file From a186bc7fbfe6b742a2392f08ae59f37662519eef Mon Sep 17 00:00:00 2001 From: F3PiX Date: Wed, 19 Aug 2015 22:26:24 +0200 Subject: [PATCH 3/9] Config database cleaner gem --- spec/rails_helper.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 42514a1..5a1f854 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -29,8 +29,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 From 84932466153313c0fb8a7a63c21cfe2817970df9 Mon Sep 17 00:00:00 2001 From: F3PiX Date: Thu, 20 Aug 2015 10:29:14 +0200 Subject: [PATCH 4/9] Adding Shoulda matchers --- Gemfile | 2 +- Gemfile.lock | 3 ++ config/locales/en.yml | 1 + spec/controllers/lessons_controller_spec.rb | 2 +- spec/factories/learning_materials.rb | 9 +++++ spec/models/learning_material_spec.rb | 41 ++++++++++++++++++++- spec/rails_helper.rb | 1 + 7 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 spec/factories/learning_materials.rb diff --git a/Gemfile b/Gemfile index df18a3c..f5c0a56 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 85ab297..3e90915 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -177,6 +179,7 @@ DEPENDENCIES rspec-rails (~> 3.0) sass-rails (~> 5.0) sdoc (~> 0.4.0) + shoulda-matchers spring sqlite3 turbolinks diff --git a/config/locales/en.yml b/config/locales/en.yml index 0653957..bee05f0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -21,3 +21,4 @@ en: hello: "Hello world" + topic: "can't be blank" diff --git a/spec/controllers/lessons_controller_spec.rb b/spec/controllers/lessons_controller_spec.rb index b13412f..9bfe4f0 100644 --- a/spec/controllers/lessons_controller_spec.rb +++ b/spec/controllers/lessons_controller_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe LessonsController, type: :controller do +describe LessonsController, type: :controller do describe "GET #index" do it "returns http success" do diff --git a/spec/factories/learning_materials.rb b/spec/factories/learning_materials.rb new file mode 100644 index 0000000..79b5f04 --- /dev/null +++ b/spec/factories/learning_materials.rb @@ -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 diff --git a/spec/models/learning_material_spec.rb b/spec/models/learning_material_spec.rb index de6613e..3fab426 100644 --- a/spec/models/learning_material_spec.rb +++ b/spec/models/learning_material_spec.rb @@ -1,5 +1,42 @@ 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) } + 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 + + diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 5a1f854..60f4a6e 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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! From f5991c13dc3cb670ee931a5f62c8e7a866da7160 Mon Sep 17 00:00:00 2001 From: F3PiX Date: Thu, 20 Aug 2015 12:23:11 +0200 Subject: [PATCH 5/9] Testing associations --- app/models/learning_material.rb | 2 +- spec/models/learning_material_spec.rb | 13 +++++++++++++ spec/models/lesson_spec.rb | 4 ++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/models/learning_material.rb b/app/models/learning_material.rb index 6322ab7..749f483 100644 --- a/app/models/learning_material.rb +++ b/app/models/learning_material.rb @@ -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 diff --git a/spec/models/learning_material_spec.rb b/spec/models/learning_material_spec.rb index 3fab426..3b69684 100644 --- a/spec/models/learning_material_spec.rb +++ b/spec/models/learning_material_spec.rb @@ -19,6 +19,19 @@ end it { should belong_to(:lesson) } + + context 'with scope' do + before(:all) do + @first = create(:learning_material, created_at: 1.day.ago) + @last = create(:learning_material, created_at: 3.day.ago) + end + + #not for daily behaviour, but f.e. when changing the dbase etc + it 'should return the materials in the correct order' do + expect(LearningMaterial.ordered).to match_array([@last, @first]) + end + end + end diff --git a/spec/models/lesson_spec.rb b/spec/models/lesson_spec.rb index fd00863..fa898f6 100644 --- a/spec/models/lesson_spec.rb +++ b/spec/models/lesson_spec.rb @@ -18,6 +18,10 @@ 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 From a4dde1d76f70b0846be55dda5379494599ab8ce6 Mon Sep 17 00:00:00 2001 From: F3PiX Date: Thu, 20 Aug 2015 12:31:27 +0200 Subject: [PATCH 6/9] Lesson should have_many lm test --- spec/models/lesson_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/lesson_spec.rb b/spec/models/lesson_spec.rb index fa898f6..2715ed7 100644 --- a/spec/models/lesson_spec.rb +++ b/spec/models/lesson_spec.rb @@ -21,7 +21,7 @@ it { should have_many(:learning_materials) } it { should have_many(:learning_materials).dependent(:destroy) } - + end From 9073a5d24d237ad018847efbc5e2f93ec039eb24 Mon Sep 17 00:00:00 2001 From: F3PiX Date: Thu, 20 Aug 2015 16:32:37 +0200 Subject: [PATCH 7/9] WIP Test with array --- spec/models/learning_material_spec.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/spec/models/learning_material_spec.rb b/spec/models/learning_material_spec.rb index 3b69684..e97b643 100644 --- a/spec/models/learning_material_spec.rb +++ b/spec/models/learning_material_spec.rb @@ -21,14 +21,16 @@ it { should belong_to(:lesson) } context 'with scope' do - before(:all) do + #not for daily behaviour, but f.e. when changing the dbase etc + it 'should return the materials in the correct order' do @first = create(:learning_material, created_at: 1.day.ago) @last = create(:learning_material, created_at: 3.day.ago) - end + #this one passes! : + #expect(LearningMaterial.ordered).to match_array([@first, @last]) - #not for daily behaviour, but f.e. when changing the dbase etc - it 'should return the materials in the correct order' do - expect(LearningMaterial.ordered).to match_array([@last, @first]) + #what is wrong with this??? tried several things + array = [@last, @first] + expect(LearningMaterial.ordered[1..2]).to eq(array) end end From d4c0facc0a5030838227fdd55fe8e27259cf5f61 Mon Sep 17 00:00:00 2001 From: F3PiX Date: Thu, 20 Aug 2015 20:50:48 +0200 Subject: [PATCH 8/9] Addins scope context to Lesson model test --- spec/controllers/lessons_controller_spec.rb | 66 +++++++++++---------- spec/models/learning_material_spec.rb | 11 +--- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/spec/controllers/lessons_controller_spec.rb b/spec/controllers/lessons_controller_spec.rb index 9bfe4f0..29fd749 100644 --- a/spec/controllers/lessons_controller_spec.rb +++ b/spec/controllers/lessons_controller_spec.rb @@ -1,33 +1,35 @@ -require 'rails_helper' +#We don't test controllers here. TEMP: Leave it here for reference -describe LessonsController, type: :controller do - - describe "GET #index" do - it "returns http success" do - get :index - expect(response).to have_http_status(:success) - end - end - - describe "GET #show" do - it "returns http success" do - get :show - expect(response).to have_http_status(:success) - end - end - - describe "GET #new" do - it "returns http success" do - get :new - expect(response).to have_http_status(:success) - end - end - - describe "GET #edit" do - it "returns http success" do - get :edit - expect(response).to have_http_status(:success) - end - end - -end +# require 'rails_helper' +# +# describe LessonsController, type: :controller do +# +# describe "GET #index" do +# it "returns http success" do +# get :index +# expect(response).to have_http_status(:success) +# end +# end +# +# describe "GET #show" do +# it "returns http success" do +# get :show +# expect(response).to have_http_status(:success) +# end +# end +# +# describe "GET #new" do +# it "returns http success" do +# get :new +# expect(response).to have_http_status(:success) +# end +# end +# +# describe "GET #edit" do +# it "returns http success" do +# get :edit +# expect(response).to have_http_status(:success) +# end +# end +# +# end diff --git a/spec/models/learning_material_spec.rb b/spec/models/learning_material_spec.rb index e97b643..18d63e7 100644 --- a/spec/models/learning_material_spec.rb +++ b/spec/models/learning_material_spec.rb @@ -23,14 +23,9 @@ 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 - @first = create(:learning_material, created_at: 1.day.ago) - @last = create(:learning_material, created_at: 3.day.ago) - #this one passes! : - #expect(LearningMaterial.ordered).to match_array([@first, @last]) - - #what is wrong with this??? tried several things - array = [@last, @first] - expect(LearningMaterial.ordered[1..2]).to eq(array) + @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 From 8b525f126df7ebd73403e26b1288fbe5a8f35d87 Mon Sep 17 00:00:00 2001 From: F3PiX Date: Thu, 20 Aug 2015 20:51:27 +0200 Subject: [PATCH 9/9] Delete controller testfile --- spec/controllers/lessons_controller_spec.rb | 35 --------------------- 1 file changed, 35 deletions(-) delete mode 100644 spec/controllers/lessons_controller_spec.rb diff --git a/spec/controllers/lessons_controller_spec.rb b/spec/controllers/lessons_controller_spec.rb deleted file mode 100644 index 29fd749..0000000 --- a/spec/controllers/lessons_controller_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -#We don't test controllers here. TEMP: Leave it here for reference - -# require 'rails_helper' -# -# describe LessonsController, type: :controller do -# -# describe "GET #index" do -# it "returns http success" do -# get :index -# expect(response).to have_http_status(:success) -# end -# end -# -# describe "GET #show" do -# it "returns http success" do -# get :show -# expect(response).to have_http_status(:success) -# end -# end -# -# describe "GET #new" do -# it "returns http success" do -# get :new -# expect(response).to have_http_status(:success) -# end -# end -# -# describe "GET #edit" do -# it "returns http success" do -# get :edit -# expect(response).to have_http_status(:success) -# end -# end -# -# end