From 18b344b66c59f0b5532da0de4566f91ae0d1f92a Mon Sep 17 00:00:00 2001 From: Mirv Date: Fri, 9 Jun 2017 11:40:15 +0000 Subject: [PATCH] recording variety of changes to get controller tests working --- .byebug_history | 22 +++++ Gemfile | 5 +- Gemfile.lock | 35 ++++++- Guardfile | 44 +++++++++ app/controllers/recipes_controller.rb | 8 +- app/models/recipe.rb | 3 + config/environments/test.rb | 3 + .../20170603191543_add_prep_to_recipes.rb | 5 + .../20170605004344_add_feeds_to_recipes.rb | 5 + ...20170605014412_add_preptimes_to_recipes.rb | 5 + ...0605014604_remove_preptime_from_recipes.rb | 5 + ...70606044516_add_servings_made_to_recipe.rb | 5 + db/schema.rb | 7 +- .../controllers/ingredient_controller_test.rb | 4 +- test/controllers/recipe_controller_test.rb | 6 -- test/controllers/recipes_controller_test.rb | 93 ++++++++++++++++++ test/fixtures/recipes.yml | 8 +- test/fixtures/users.yml | 2 + test/models/chef_test.rb | 98 +++++++++---------- test/models/recipe_test.rb | 15 ++- test/test_helper.rb | 13 +++ 21 files changed, 320 insertions(+), 71 deletions(-) create mode 100644 Guardfile create mode 100644 db/migrate/20170603191543_add_prep_to_recipes.rb create mode 100644 db/migrate/20170605004344_add_feeds_to_recipes.rb create mode 100644 db/migrate/20170605014412_add_preptimes_to_recipes.rb create mode 100644 db/migrate/20170605014604_remove_preptime_from_recipes.rb create mode 100644 db/migrate/20170606044516_add_servings_made_to_recipe.rb delete mode 100644 test/controllers/recipe_controller_test.rb create mode 100644 test/controllers/recipes_controller_test.rb diff --git a/.byebug_history b/.byebug_history index 83668df..bf3c076 100644 --- a/.byebug_history +++ b/.byebug_history @@ -1,3 +1,25 @@ +exit +step +exps +e.call +e +before +step +exps +step +exps +e +step +expression +step +@user +Recipe.count +exit +response.body +response +exit +@recipe +recipe q! eval raise(exception) eval e diff --git a/Gemfile b/Gemfile index dcea50b..47d55f1 100644 --- a/Gemfile +++ b/Gemfile @@ -41,13 +41,16 @@ gem 'raphael-rails' group :development do gem 'web-console', '~> 2.0' - gem 'spring' + # gem 'spring' end group :development, :test do gem 'sqlite3' gem 'pry' gem 'byebug' + gem 'guard' + gem 'guard-minitest' + end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 054a627..1b86ae2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -94,6 +94,7 @@ GEM execjs (2.7.0) faraday (0.11.0) multipart-post (>= 1.2, < 3) + ffi (1.9.18) figaro (1.1.1) thor (~> 0.14) fission (0.5.0) @@ -228,6 +229,19 @@ GEM formatador (0.2.5) globalid (0.3.7) activesupport (>= 4.1.0) + guard (2.14.1) + 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-minitest (2.4.6) + guard-compat (~> 1.2) + minitest (>= 3.0) hashie (3.5.5) i18n (0.8.1) inflecto (0.0.2) @@ -243,8 +257,13 @@ GEM railties (>= 3.2.16) json (1.8.6) jwt (1.5.6) + listen (3.1.5) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + ruby_dep (~> 1.2) loofah (2.0.3) nokogiri (>= 1.5.9) + lumberjack (1.0.12) mail (2.6.4) mime-types (>= 1.16, < 4) mailboxer (0.14.0) @@ -262,9 +281,13 @@ GEM multi_json (1.12.1) multi_xml (0.6.0) multipart-post (2.0.0) + nenv (0.3.0) net-ssh (3.2.0) nokogiri (1.7.1) mini_portile2 (~> 2.1.0) + notiffany (0.1.1) + nenv (~> 0.1) + shellany (~> 0.0) oauth (0.5.1) oauth2 (1.3.1) faraday (>= 0.8, < 0.12) @@ -330,6 +353,9 @@ GEM ranked-model (0.4.0) activerecord (>= 3.1.12) raphael-rails (2.1.2) + rb-fsevent (0.9.8) + rb-inotify (0.9.8) + ffi (>= 0.5.0) rbvmomi (1.11.0) builder (~> 3.0) json (>= 1.8) @@ -338,6 +364,7 @@ GEM rdoc (4.3.0) responders (2.3.0) railties (>= 4.2.0, < 5.1) + ruby_dep (1.5.0) sass (3.4.23) sass-rails (5.0.6) railties (>= 4.0.0, < 6) @@ -352,6 +379,7 @@ GEM activemodel (>= 4.1) elasticsearch (>= 1) hashie + shellany (0.0.1) simple_form (3.4.0) actionpack (> 4, < 5.1) activemodel (> 4, < 5.1) @@ -359,8 +387,6 @@ GEM social-share-button (0.1.10) coffee-rails sass-rails - spring (2.0.1) - activesupport (>= 4.2) sprockets (3.7.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -413,6 +439,8 @@ DEPENDENCIES figaro fog (~> 1.38.0) font-awesome-sass (~> 4.6.2) + guard + guard-minitest jbuilder (~> 2.0) jquery-rails jquery-ui-rails (~> 5.0, >= 5.0.5) @@ -435,7 +463,6 @@ DEPENDENCIES searchkick simple_form social-share-button - spring sqlite3 turbolinks uglifier (>= 1.3.0) @@ -447,4 +474,4 @@ RUBY VERSION ruby 2.3.0p0 BUNDLED WITH - 1.13.6 + 1.14.4 diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..8a2d82f --- /dev/null +++ b/Guardfile @@ -0,0 +1,44 @@ +# 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" + + +guard :minitest do + # with Minitest::Unit + watch(%r{^test/(.*)\/?test_(.*)\.rb$}) + watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" } + watch(%r{^test/test_helper\.rb$}) { 'test' } + + # with Minitest::Spec + # watch(%r{^spec/(.*)_spec\.rb$}) + # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + # watch(%r{^spec/spec_helper\.rb$}) { 'spec' } + + # Rails 4 + watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" } + watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' } + watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" } + watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" } + watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" } + watch(%r{^test/.+_test\.rb$}) + watch(%r{^test/test_helper\.rb$}) { 'test' } + watch(%r{^test/controllers/*.rb}) + + # Rails < 4 + # watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" } + # watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" } + # watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" } +end diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 6c9e4ce..e97d3ed 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -5,8 +5,7 @@ class RecipesController < ApplicationController before_action :authenticate_user!, except: [:show, :index, :like, :search] before_action :require_same_user, only: [:edit, :update] before_action :admin_user, only: :destroy - - + def search if params[:search].present? @recipes = Recipe.search(params[:search]) @@ -93,9 +92,10 @@ def set_user @user = User.find(current_user.id) end - + # name: , summary: , description: , prep_times: , servings_made: def recipe_params - params.require(:recipe).permit(:user_id, :chef_id, :name, :summary, :description, :picture, style_ids: [], ingredients_attributes: [:id, :name, :_destroy], directions_attributes: [:id, :step, :_destroy], feed_ids: [], calorie_ids: [], preptime_ids: []) + params.require(:recipe).permit(:id, :user_id, :name, :summary, :description, + :prep_times, :servings_made ) end def set_recipe diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 58af3c1..97d6712 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -26,6 +26,9 @@ class Recipe < ActiveRecord::Base # validates :directions, presence: true # validates :ingredients, presence: true + # validates :feeds, presence: true + # validates :servings_made, presence: true + # validates :prep_times, presence: true validates :name, presence: true, length: { minimum: 5, maximum: 100} validates :summary, presence: true, length: { minimum: 10, maximum: 150} validates :description, presence: true, length: { minimum: 5, maximum: 1000} diff --git a/config/environments/test.rb b/config/environments/test.rb index 1c19f08..4177e66 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -39,4 +39,7 @@ # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + config.log_level = :debug + end diff --git a/db/migrate/20170603191543_add_prep_to_recipes.rb b/db/migrate/20170603191543_add_prep_to_recipes.rb new file mode 100644 index 0000000..7584628 --- /dev/null +++ b/db/migrate/20170603191543_add_prep_to_recipes.rb @@ -0,0 +1,5 @@ +class AddPrepToRecipes < ActiveRecord::Migration + def change + add_column :recipes, :prep_time, :integer + end +end diff --git a/db/migrate/20170605004344_add_feeds_to_recipes.rb b/db/migrate/20170605004344_add_feeds_to_recipes.rb new file mode 100644 index 0000000..3412d41 --- /dev/null +++ b/db/migrate/20170605004344_add_feeds_to_recipes.rb @@ -0,0 +1,5 @@ +class AddFeedsToRecipes < ActiveRecord::Migration + def change + add_column :recipes, :feeds, :integer + end +end diff --git a/db/migrate/20170605014412_add_preptimes_to_recipes.rb b/db/migrate/20170605014412_add_preptimes_to_recipes.rb new file mode 100644 index 0000000..9cdd000 --- /dev/null +++ b/db/migrate/20170605014412_add_preptimes_to_recipes.rb @@ -0,0 +1,5 @@ +class AddPreptimesToRecipes < ActiveRecord::Migration + def change + add_column :recipes, :prep_times, :integer + end +end diff --git a/db/migrate/20170605014604_remove_preptime_from_recipes.rb b/db/migrate/20170605014604_remove_preptime_from_recipes.rb new file mode 100644 index 0000000..6389c50 --- /dev/null +++ b/db/migrate/20170605014604_remove_preptime_from_recipes.rb @@ -0,0 +1,5 @@ +class RemovePreptimeFromRecipes < ActiveRecord::Migration + def change + remove_column :recipes, :prep_time + end +end diff --git a/db/migrate/20170606044516_add_servings_made_to_recipe.rb b/db/migrate/20170606044516_add_servings_made_to_recipe.rb new file mode 100644 index 0000000..171e0df --- /dev/null +++ b/db/migrate/20170606044516_add_servings_made_to_recipe.rb @@ -0,0 +1,5 @@ +class AddServingsMadeToRecipe < ActiveRecord::Migration + def change + add_column :recipes, :servings_made, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index bce48b0..1c84deb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170413232840) do +ActiveRecord::Schema.define(version: 20170606044516) do create_table "directions", force: :cascade do |t| t.text "step" @@ -180,6 +180,9 @@ t.datetime "updated_at" t.string "picture" t.integer "user_id" + t.integer "feeds" + t.integer "prep_times" + t.integer "servings_made" end add_index "recipes", ["user_id"], name: "index_recipes_on_user_id" @@ -275,4 +278,4 @@ add_index "zets", ["exercise_id"], name: "index_zets_on_exercise_id" add_index "zets", ["workout_id"], name: "index_zets_on_workout_id" -end \ No newline at end of file +end diff --git a/test/controllers/ingredient_controller_test.rb b/test/controllers/ingredient_controller_test.rb index a67648e..9a1b11a 100644 --- a/test/controllers/ingredient_controller_test.rb +++ b/test/controllers/ingredient_controller_test.rb @@ -1,12 +1,12 @@ require 'test_helper' class IngredientControllerTest < ActionController::TestCase - test "should get name:string" do + test "should get name" do get :name assert_response :success end - test "should get recipe_id:integer" do + test "should get recipe_id" do get :recipe_id assert_response :success end diff --git a/test/controllers/recipe_controller_test.rb b/test/controllers/recipe_controller_test.rb deleted file mode 100644 index 230e8c8..0000000 --- a/test/controllers/recipe_controller_test.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'test_helper' - -class RecipeControllerTest < ActionController::TestCase - -end - diff --git a/test/controllers/recipes_controller_test.rb b/test/controllers/recipes_controller_test.rb new file mode 100644 index 0000000..6d5121b --- /dev/null +++ b/test/controllers/recipes_controller_test.rb @@ -0,0 +1,93 @@ +require 'test_helper' + +# class RecipesControllerTest < ActionController::TestCase +class RecipesControllerTest < ActionDispatch::IntegrationTest + setup do + # sign_in could be @user or users(:one) if we have fixtures + @user = users(:one) + sign_in @user + # @recipe = recipes(:one) + end + + # test "should get index" do + # get :index + # assert_response :success + # # assert_not_nil assigns(:recipes) + # end + + # test "should get new" do + # get :new + # assert_response :success + # end + + test "should create recipe" do + assert_difference('Recipe.count') do + # This next line obviously will fail - since it's just taking an existing record + # post :create, recipe: @recipe + + # Bad request issue here...perhaps Rails 5 format in rails 4 app? + # post :create, params: { recipe: { name: "SOMEONE", summary: "Something Else", + # description: "Something Else", prep_times: "90", servings_made: "3"}} + post recipes_url, params: { recipe: { name: "SOMEONE", summary: "Something Else", + description: "Something Else", prep_times: "90", servings_made: "3"} } + + ### Bad request after user load x2 + # post :create, params: { name: "SOMEONE", summary: "Something Else", + # description: "Something Else", prep_times: "90", servings_made: "3"} + + ### Bad request after user load x2 + # post :create, params: { name: "SOMEONE", summary: "Something Else", + # description: "Something Else", prep_times: "90", servings_made: "3"} + end + # assert_redirected_to recipe_path(assigns(:recipe)) + + assert_redirected_to recipe_path(@recipe) + end + + # test "should show recipe" do + # get :show, id: @recipe + # assert_response :success + # end + + # test "should get edit" do + # get :edit, id: @recipe + # assert_response :success + # end + + # test "should update recipe" do + # patch :update, id: @recipe, recipe: @recipe + # assert_redirected_to recipe_path(assigns(:recipe)) + # end + + # test "should destroy recipe" do + # @recipe = Recipe.create!(name: "SOMEONE", summary: "Something Else", + # description: "Something Else", prep_times: "90", servings_made: "3") + # assert_difference('Recipe.count', -1) do + # recipe_path(@recipe, method: :delete) + # end + + # assert_redirected_to recipes_path + # end + + + # test "should destroy recipe" do + # @recipe = Recipe.create!(name: "SOMEONE", summary: "Something Else", + # description: "Something Else", prep_times: "90", servings_made: "3") + # assert_difference('Recipe.count', -1) do + # puts "\n\nRecipe: #{@recipe} -- User: #{@user.username}, #{@user.email}, #{@user.admin}\n\n" + # recipe_path(@recipe, method: :delete) + # end + + # assert_redirected_to recipe_url + # end + +end + + + # test "should destroy country" do + # assert_difference('Country.count', -1) do + # delete country_url(@country) + # end + + # assert_redirected_to countries_url + # end \ No newline at end of file diff --git a/test/fixtures/recipes.yml b/test/fixtures/recipes.yml index a812e2a..4d53b0e 100644 --- a/test/fixtures/recipes.yml +++ b/test/fixtures/recipes.yml @@ -4,12 +4,16 @@ one: name: MyString1 summary: MyText2 description: MyText - picture: + prep_times: MyTest + servings_made: 10 + # picture: user_id: one - # ingredients: one two: name: MyString2 summary: MyText2 description: MyText + prep_times: MyTest + servings_made: 10 + # picture: user_id: one diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 1e5ba36..02c1b2b 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -7,9 +7,11 @@ one: username: MyString email: MyEmail@email.com + admin: true # column: value # two: username: MyString2 email: MyEmail2@email.com + admin: false # column: value diff --git a/test/models/chef_test.rb b/test/models/chef_test.rb index 9d63342..6411320 100644 --- a/test/models/chef_test.rb +++ b/test/models/chef_test.rb @@ -1,62 +1,62 @@ -require "test_helper" +# require "test_helper" -class ChefTest < ActiveSupport::TestCase +# class ChefTest < ActiveSupport::TestCase - def setup - @chef = Chef.new(chefname: "John", email: "john@example.com") - end +# def setup +# @chef = Chef.new(chefname: "John", email: "john@example.com") +# end - test "chefname should be valid" do - assert @chef.valid? - end +# test "chefname should be valid" do +# assert @chef.valid? +# end - test "chefname should be present" do - @chef.chefname = " " - assert_not @chef.valid? - end +# test "chefname should be present" do +# @chef.chefname = " " +# assert_not @chef.valid? +# end - test "chefname should not be too long" do - @chef.chefname = "a" * 41 - assert_not @chef.valid? - end +# test "chefname should not be too long" do +# @chef.chefname = "a" * 41 +# assert_not @chef.valid? +# end - test "chefname should not be too short" do - @chef.chefname = "aa" - assert_not @chef.valid? - end +# test "chefname should not be too short" do +# @chef.chefname = "aa" +# assert_not @chef.valid? +# end - test "email must be present" do - @chef.email = "" - assert_not @chef.valid? - end +# test "email must be present" do +# @chef.email = "" +# assert_not @chef.valid? +# end - test "email length should be within bounds" do - @chef.email = "a" * 101 + "@example.com" - assert_not @chef.valid? - end +# test "email length should be within bounds" do +# @chef.email = "a" * 101 + "@example.com" +# assert_not @chef.valid? +# end - test "email address should be unique" do - dup_chef = @chef.dup - dup_chef.email = @chef.email.upcase - @chef.save - assert_not dup_chef.valid? - end +# test "email address should be unique" do +# dup_chef = @chef.dup +# dup_chef.email = @chef.email.upcase +# @chef.save +# assert_not dup_chef.valid? +# end - test "email validation should accept valid address " do - valid_addresses = %w[user@eee.com R_TDD-DS@eee.hello.org user@example.com first.last@eem.au laura+joe@monl.cm] - valid_addresses.each do |va| - @chef.email = va - assert @chef.valid?, '#{va.inspect} should be valid' - end - end +# test "email validation should accept valid address " do +# valid_addresses = %w[user@eee.com R_TDD-DS@eee.hello.org user@example.com first.last@eem.au laura+joe@monl.cm] +# valid_addresses.each do |va| +# @chef.email = va +# assert @chef.valid?, '#{va.inspect} should be valid' +# end +# end - test "should reject invalid email address" do - invalid_addresses =%w[user@example,com user_at_eee_org user.name@example. ee@i_am_.com foo@ee+aar.com] - invalid_addresses.each do |ia| - @chef.email = ia - assert_not @chef.valid?, '#{ia.inspect} should be invalid' - end -end +# test "should reject invalid email address" do +# invalid_addresses =%w[user@example,com user_at_eee_org user.name@example. ee@i_am_.com foo@ee+aar.com] +# invalid_addresses.each do |ia| +# @chef.email = ia +# assert_not @chef.valid?, '#{ia.inspect} should be invalid' +# end +# end @@ -64,4 +64,4 @@ def setup -end \ No newline at end of file +# end \ No newline at end of file diff --git a/test/models/recipe_test.rb b/test/models/recipe_test.rb index 3df4a0c..acf1ba2 100644 --- a/test/models/recipe_test.rb +++ b/test/models/recipe_test.rb @@ -6,7 +6,8 @@ class RecipeTest < ActiveSupport::TestCase # end test "recipe is valid" do - recipe = Recipe.new(name: "SOMEONE", summary: "Something Else", description: "Something Else") + recipe = Recipe.new(name: "SOMEONE", summary: "Something Else", + description: "Something Else", prep_times: "90", servings_made: "3") assert recipe.valid?, 'recipe is not valid' end @@ -24,4 +25,16 @@ class RecipeTest < ActiveSupport::TestCase recipe = Recipe.new(summary: "Something Else", description: "Something Else") assert recipe.invalid?, 'recipe should be invalid, but passed validation' end + + test "recipe should be invalid without preptime" do + recipe = Recipe.new(name: "Something new", summary: "Something Else", + description: "Something Else", servings_made: "3") + assert recipe.invalid?, 'recipe should be invalid - missing prep_times, but passed validation' + end + + test "recipe should be invalid without servings_made" do + recipe = Recipe.new(name: "SOMEONE", summary: "Something Else", + description: "Something Else", prep_times: "Something") + refute recipe.valid?, 'recipe should be invalid - missing servings_made, but passed validation' + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 92e39b2..443d49e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,3 +8,16 @@ class ActiveSupport::TestCase # Add more helper methods to be used by all tests here... end + + +class ActionController::TestCase + + include Devise::Test::ControllerHelpers + +end + +class ActionDispatch::IntegrationTest + + include Devise::Test::IntegrationHelpers + +end \ No newline at end of file