From fbb0266f39301362a0189207478623e556eefeb5 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 22 Apr 2015 01:28:19 -0700 Subject: [PATCH 01/10] update rspec rails in gemfile --- Gemfile | 2 +- Gemfile.lock | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index b943ec6..a773947 100644 --- a/Gemfile +++ b/Gemfile @@ -42,7 +42,7 @@ gem 'rack-cache' group :development, :test do # Cucumber, testing and coverage gem 'cucumber-rails', :require => false - gem 'rspec-rails', '~> 2.14.0' + gem 'rspec-rails', '>= 3.1' gem 'simplecov' gem "factory_girl_rails" gem 'faker' diff --git a/Gemfile.lock b/Gemfile.lock index 5c3218c..d52e1be 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -202,18 +202,23 @@ GEM http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 3.0) netrc (~> 0.7) - rspec-core (2.14.8) - rspec-expectations (2.14.5) - diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.14.6) - rspec-rails (2.14.2) - actionpack (>= 3.0) - activemodel (>= 3.0) - activesupport (>= 3.0) - railties (>= 3.0) - rspec-core (~> 2.14.0) - rspec-expectations (~> 2.14.0) - rspec-mocks (~> 2.14.0) + rspec-core (3.2.3) + rspec-support (~> 3.2.0) + rspec-expectations (3.2.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.2.0) + rspec-mocks (3.2.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.2.0) + rspec-rails (3.2.1) + actionpack (>= 3.0, < 4.3) + activesupport (>= 3.0, < 4.3) + railties (>= 3.0, < 4.3) + rspec-core (~> 3.2.0) + rspec-expectations (~> 3.2.0) + rspec-mocks (~> 3.2.0) + rspec-support (~> 3.2.0) + rspec-support (3.2.2) ruby2ruby (2.1.3) ruby_parser (~> 3.1) sexp_processor (~> 4.0) @@ -301,7 +306,7 @@ DEPENDENCIES rack-cache rails (= 4.2.1) rails_12factor - rspec-rails (~> 2.14.0) + rspec-rails (>= 3.1) sass-rails (~> 5.0) sdoc (~> 0.4.0) shoulda-matchers From f22af85bf8ece51c8804a16a05abe4e5567556c7 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 22 Apr 2015 02:07:18 -0700 Subject: [PATCH 02/10] mv file for rspec 3 migration --- Gemfile | 4 +++- spec/controllers/api/v1/users_controller_spec.rb | 7 +++++-- spec/{spec_helper.rb => rails_helper.rb} | 0 3 files changed, 8 insertions(+), 3 deletions(-) rename spec/{spec_helper.rb => rails_helper.rb} (100%) diff --git a/Gemfile b/Gemfile index a773947..7123037 100644 --- a/Gemfile +++ b/Gemfile @@ -42,7 +42,9 @@ gem 'rack-cache' group :development, :test do # Cucumber, testing and coverage gem 'cucumber-rails', :require => false - gem 'rspec-rails', '>= 3.1' + gem 'rspec-rails', '>= 3.1 + + ' gem 'simplecov' gem "factory_girl_rails" gem 'faker' diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index 4524eb0..ca9d2e9 100644 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -3,9 +3,12 @@ require 'registrations_controller' require 'api/v1/users_controller' -describe Api::V1::UsersController do +describe Api::V1::UsersController, :type => :request do - before(:each) { request.headers['Accept'] = "application/json" } + before(:each) do + puts @request + # @request.headers['Accept'] = "application/json" + end describe "GET #show" do before(:each) do diff --git a/spec/spec_helper.rb b/spec/rails_helper.rb similarity index 100% rename from spec/spec_helper.rb rename to spec/rails_helper.rb From 6db5e85d2b08599ee05aa9159eee5a1f5a00eec3 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 22 Apr 2015 02:11:49 -0700 Subject: [PATCH 03/10] update to rspec 3 pt 2 - new rails_helper --- spec/rails_helper.rb | 65 +++++++++++++++++++-------------- spec/spec_helper.rb | 87 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 28 deletions(-) create mode 100644 spec/spec_helper.rb diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 2ccff72..956406f 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,19 +1,32 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' -ENV["RAILS_ENV"] ||= 'test' +ENV['RAILS_ENV'] ||= 'test' +require 'spec_helper' +require File.expand_path('../../config/environment', __FILE__) +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +require 'devise' + require 'simplecov' SimpleCov.start require 'coveralls' Coveralls.wear! -require File.expand_path("../../config/environment", __FILE__) -require 'rspec/rails' -require 'rspec/autorun' -require 'devise' - -# Requires supporting ruby files with custom matchers and macros, etc, -# in spec/support/ and its subdirectories. -Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the -pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } # Checks for pending migrations before tests are run. # If you are not using ActiveRecord, you can remove this line. @@ -21,14 +34,7 @@ RSpec.configure do |config| config.include Devise::TestHelpers, type: :controller - # ## Mock Framework - # - # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: - # - # config.mock_with :mocha - # config.mock_with :flexmock - # config.mock_with :rr - + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" @@ -37,15 +43,18 @@ # instead of true. config.use_transactional_fixtures = true - # If true, the base class of anonymous controllers will be inferred - # automatically. This will be the default behavior in future versions of - # rspec-rails. - config.infer_base_class_for_anonymous_controllers = false - - # Run specs in random order to surface order dependencies. If you find an - # order dependency and want to debug it, you can fix the order by providing - # the seed, which is printed after each run. - # --seed 1234 - config.order = "random" - + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, :type => :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..38a4f9a --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,87 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# The `.rspec` file also contains a few flags that are not defaults but that +# users commonly want. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # These two settings work together to allow you to limit a spec run + # to individual examples or groups you care about by tagging them with + # `:focus` metadata. When nothing is tagged with `:focus`, all examples + # get run. + config.filter_run :focus + config.run_all_when_everything_filtered = true + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax + # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = 'doc' + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end From 4e0910f94543fd5808883b189944fd40237abd23 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 22 Apr 2015 02:13:00 -0700 Subject: [PATCH 04/10] new require statements for specs --- lib/spec/api_constrains_spec.rb | 2 +- spec/controllers/api/v1/projects_controller_spec.rb | 2 +- spec/controllers/api/v1/users_controller_spec.rb | 2 +- spec/controllers/assignments_controller_spec.rb | 2 +- spec/controllers/courses_controller_spec.rb | 2 +- spec/controllers/pages_controller_spec.rb | 2 +- spec/controllers/projects_controller_spec.rb | 2 +- spec/controllers/submissions_controller_spec.rb | 2 +- spec/controllers/user_controller_spec.rb | 2 +- spec/models/announcement_spec.rb | 2 +- spec/models/assignment_spec.rb | 2 +- spec/models/course_spec.rb | 2 +- spec/models/enrollment_spec.rb | 2 +- spec/models/project_spec.rb | 2 +- spec/models/user_spec.rb | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/spec/api_constrains_spec.rb b/lib/spec/api_constrains_spec.rb index 8760d4f..bc5337b 100644 --- a/lib/spec/api_constrains_spec.rb +++ b/lib/spec/api_constrains_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe ApiConstraints do let(:api_constraints_v1) { ApiConstraints.new(version: 1) } diff --git a/spec/controllers/api/v1/projects_controller_spec.rb b/spec/controllers/api/v1/projects_controller_spec.rb index d6d9d4b..e0f2aa3 100644 --- a/spec/controllers/api/v1/projects_controller_spec.rb +++ b/spec/controllers/api/v1/projects_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' require 'api/v1/projects_controller' diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index ca9d2e9..b214b1f 100644 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' require 'sessions_controller' require 'registrations_controller' require 'api/v1/users_controller' diff --git a/spec/controllers/assignments_controller_spec.rb b/spec/controllers/assignments_controller_spec.rb index 1372cb0..4bb8449 100644 --- a/spec/controllers/assignments_controller_spec.rb +++ b/spec/controllers/assignments_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe AssignmentsController do diff --git a/spec/controllers/courses_controller_spec.rb b/spec/controllers/courses_controller_spec.rb index f8ec369..078fa62 100644 --- a/spec/controllers/courses_controller_spec.rb +++ b/spec/controllers/courses_controller_spec.rb @@ -1 +1 @@ -require 'spec_helper' +require 'rails_helper' diff --git a/spec/controllers/pages_controller_spec.rb b/spec/controllers/pages_controller_spec.rb index d8d62cd..f2c3252 100644 --- a/spec/controllers/pages_controller_spec.rb +++ b/spec/controllers/pages_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe PagesController do diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb index 6c7a626..bcacfde 100644 --- a/spec/controllers/projects_controller_spec.rb +++ b/spec/controllers/projects_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe ProjectsController do diff --git a/spec/controllers/submissions_controller_spec.rb b/spec/controllers/submissions_controller_spec.rb index c027d23..aafac7e 100644 --- a/spec/controllers/submissions_controller_spec.rb +++ b/spec/controllers/submissions_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe SubmissionsController do diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index 142455c..f915ae6 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe UsersController do diff --git a/spec/models/announcement_spec.rb b/spec/models/announcement_spec.rb index 50d1cc1..59af17e 100644 --- a/spec/models/announcement_spec.rb +++ b/spec/models/announcement_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe Announcement do pending "add some examples to (or delete) #{__FILE__}" diff --git a/spec/models/assignment_spec.rb b/spec/models/assignment_spec.rb index 3c87daf..8bf791e 100644 --- a/spec/models/assignment_spec.rb +++ b/spec/models/assignment_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe Assignment do pending "add some examples to (or delete) #{__FILE__}" diff --git a/spec/models/course_spec.rb b/spec/models/course_spec.rb index a0c5505..76835e9 100644 --- a/spec/models/course_spec.rb +++ b/spec/models/course_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe Course do describe "#students" do diff --git a/spec/models/enrollment_spec.rb b/spec/models/enrollment_spec.rb index 1d09f66..14a46fe 100644 --- a/spec/models/enrollment_spec.rb +++ b/spec/models/enrollment_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe Enrollment do pending "add some examples to (or delete) #{__FILE__}" diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 9daf08f..ba16a2b 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe Project do before { @project = FactoryGirl.build(:project) } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 8a995f3..95de256 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe User do before { @user = FactoryGirl.build(:user) } From 7c629cefefd423fde88bcf3174bdf9a2c85794b2 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 22 Apr 2015 02:19:31 -0700 Subject: [PATCH 05/10] rspec 3 upgrade progress, user api controller passing --- spec/controllers/api/v1/users_controller_spec.rb | 13 +++++-------- spec/rails_helper.rb | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index b214b1f..43eab3b 100644 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -3,12 +3,9 @@ require 'registrations_controller' require 'api/v1/users_controller' -describe Api::V1::UsersController, :type => :request do - - before(:each) do - puts @request - # @request.headers['Accept'] = "application/json" - end +describe Api::V1::UsersController, :type => :controller do + # + before(:each) { request.headers['Accept'] = "application/json" } describe "GET #show" do before(:each) do @@ -17,7 +14,7 @@ allow(controller).to receive(:current_user).and_return(fakeuser) @user = FactoryGirl.create :user - get :show, id: @user.id, format: :json + get :show, id: @user.id end @@ -38,7 +35,7 @@ allow(request.env['warden']).to receive(:authenticate!).and_return(fakeuser) allow(controller).to receive(:current_user).and_return(fakeuser) - post :create, { user: @user_attrs }, format: :json + post :create, { user: @user_attrs } end it "renders the json representation for the user record just created" do diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 956406f..c755bc2 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,6 +1,6 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' ENV['RAILS_ENV'] ||= 'test' -require 'spec_helper' +require 'rails_helper' require File.expand_path('../../config/environment', __FILE__) require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! From 506c83be4a458aaae56757c110f174bdd2f8ecd7 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 22 Apr 2015 02:21:21 -0700 Subject: [PATCH 06/10] wring the code dry --- spec/controllers/api/v1/users_controller_spec.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index 43eab3b..3abf20b 100644 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -3,8 +3,8 @@ require 'registrations_controller' require 'api/v1/users_controller' -describe Api::V1::UsersController, :type => :controller do - # +describe Api::V1::UsersController do + before(:each) { request.headers['Accept'] = "application/json" } describe "GET #show" do @@ -57,7 +57,7 @@ allow(controller).to receive(:current_user).and_return(fakeuser) - post :create, { user: @invalid_user_attributes }, format: :json + post :create, { user: @invalid_user_attributes } end it "renders json with errors" do @@ -82,11 +82,10 @@ allow(request.env['warden']).to receive(:authenticate!).and_return(fakeuser) allow(controller).to receive(:current_user).and_return(fakeuser) - delete :destroy, { id: @user.id }, format: :json + delete :destroy, { id: @user.id } end it { should respond_with 204 } - end describe "PUT/PATCH #update" do @@ -100,7 +99,7 @@ allow(controller).to receive(:current_user).and_return(fakeuser) patch :update, { id: @user.id, - user: { email: "newmail@example.com" } }, format: :json + user: { email: "newmail@example.com" } } end @@ -122,7 +121,7 @@ patch :update, { id: @user.id, - user: { email: "bademail.com" } }, format: :json + user: { email: "bademail.com" } } end it "renders an errors json" do From 9aa6d1e65f01f49f1d35f6e7a78ddf27c5240c78 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 22 Apr 2015 02:55:58 -0700 Subject: [PATCH 07/10] synatx formatting and minor cleanup --- .../api/v1/projects_controller_spec.rb | 42 ++++++++----------- .../api/v1/users_controller_spec.rb | 5 +-- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/spec/controllers/api/v1/projects_controller_spec.rb b/spec/controllers/api/v1/projects_controller_spec.rb index e0f2aa3..ec967b6 100644 --- a/spec/controllers/api/v1/projects_controller_spec.rb +++ b/spec/controllers/api/v1/projects_controller_spec.rb @@ -14,7 +14,7 @@ allow(request.env['warden']).to receive(:authenticate!).and_return(fakeuser) allow(controller).to receive(:current_user).and_return(fakeuser) - get :show, id: @project.id, format: :json + get :show, id: @project.id end it { should respond_with 200 } @@ -38,14 +38,14 @@ proj = Project.create(title: "Test proj", owner:user.id) Project.any_instance.should_receive(:update_attributes) Api::V1::ProjectsController.any_instance.stub(:getCurrentUser).and_return(user) - put :update, { project_params: proj.attributes, id:proj.id }, format: :json + put :update, { project_params: proj.attributes, id:proj.id } expect(response.status).to eq(204) end it "should reject the request when I am not logged in" do proj = Project.create(title: "Test proj") Api::V1::ProjectsController.any_instance.stub(:getCurrentUser).and_return(nil) - put :update, { project_params: proj.attributes, id:proj.id}, format: :json + put :update, { project_params: proj.attributes, id:proj.id} expect(response.status).to eq(401) end @@ -54,7 +54,7 @@ not_users_id = user.id+1 proj = Project.create(title: "Test proj", owner:(not_users_id)) Api::V1::ProjectsController.any_instance.stub(:getCurrentUser).and_return(user) - put :update, { project_params: proj.attributes, id:proj.id }, format: :json + put :update, { project_params: proj.attributes, id:proj.id } expect(response.status).to eq(401) end @@ -62,7 +62,7 @@ user = User.create(email: "steven@berk.edu") proj = Project.create(title: "Test proj", owner:user.id) Api::V1::ProjectsController.any_instance.stub(:getCurrentUser).and_return(user) - put :update, { project_params: proj.attributes, id:(proj.id+1) }, format: :json + put :update, { project_params: proj.attributes, id:(proj.id+1) } expect(response.status).to eq(404) end end @@ -83,12 +83,9 @@ Api::V1::ProjectsController.any_instance.stub(:getCurrentUser).and_return(user1) post :create, {project_params: {title: "test proj for user 1", owner:user1.id}} - expect(response.status).to eq(200) #:ok + expect(response.status).to eq(200) end - - - end describe "DELETE #destroy" do @@ -109,22 +106,19 @@ Api::V1::ProjectsController.any_instance.stub(:getCurrentUser).and_return(user1) user1_proj = Project.create(title: "user1 proj", owner:user1.id) - delete :destroy, {id: user1_proj.id} - #project_response = JSON.parse(response.body, symbolize_names: true) - ## response is populated with last response; last line is unnecessary for now - expect(response.status).to eq(200) #:ok + delete :destroy, { id: user1_proj.id } + expect(response.status).to eq(200) end it "should reject the request if user is not an owner" do user1 = User.create(email: "linda@berk.edu") user2 = User.create(email: "ellen@berk.edu") Api::V1::ProjectsController.any_instance.stub(:getCurrentUser).and_return(user2) - user1_proj = Project.create(title: "user1 proj", owner:user1.id) + user1_proj = Project.create(title: "user1 proj", owner: user1.id) - delete :destroy, {id: user1_proj.id} - #project_response = JSON.parse(response.body, symbolize_names: true) - ## response is populated with last response; last line is unnecessary for now - expect(response.status).to eq(401) #:unauthorized + delete :destroy, { id: user1_proj.id } + # FIXME -- Should probably be a 404. + expect(response.status).to eq(401) end end @@ -148,7 +142,7 @@ proj2 = Project.create(title: "user private", owner:user.id, is_public: 0) proj3 = Project.create(title: "nonuser public", owner:not_users_id, is_public: 1) proj4 = Project.create(title: "nonuser private", owner:not_users_id, is_public: 0) - get :index, {user_id: user.id}, format: :json + get :index, {user_id: user.id} project_response = JSON.parse(response.body, symbolize_names: true) expect(project_response.length).to eq(2) @@ -156,7 +150,7 @@ expect(project_response[1][:title]).to eq("user private") end - it "Should show only public projects if user not logged in" do + it "should show only public projects if user not logged in" do user = User.create(email: "jwang@berk.edu") not_users_id = user.id+1 Api::V1::ProjectsController.any_instance.stub(:getCurrentUser).and_return(nil) @@ -164,7 +158,7 @@ proj2 = Project.create(title: "user private", owner:user.id, is_public: 0) proj3 = Project.create(title: "nonuser public", owner:not_users_id, is_public: 1) proj4 = Project.create(title: "nonuser private", owner:not_users_id, is_public: 0) - get :index, {user_id: user.id}, format: :json + get :index, {user_id: user.id} project_response = JSON.parse(response.body, symbolize_names: true) expect(project_response.length).to eq(1) expect(project_response[0][:title]).to eq("user public") @@ -177,8 +171,7 @@ # before(:each) do # @project = FactoryGirl.create :project # patch :update, { id: @project.id, - # project: { title: "new title" } }, format: :json - # end + # project: { title: "new title" } } # end # it "renders the json representation for the updated project" do # project_response = JSON.parse(response.body, symbolize_names: true) @@ -192,8 +185,7 @@ # before(:each) do # @project = FactoryGirl.create :project # patch :update, { id: @project.id, - # project: { title: "bad title" } }, format: :json - # end + # project: { title: "bad title" } } # end # it "renders an errors json" do # project_response = JSON.parse(response.body, symbolize_names: true) diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index 3abf20b..6c91405 100644 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -120,11 +120,10 @@ allow(controller).to receive(:current_user).and_return(fakeuser) - patch :update, { id: @user.id, - user: { email: "bademail.com" } } + patch :update, { id: @user.id, user: { email: "bademail.com" } } end - it "renders an errors json" do + it "rendered json with errors" do user_response = JSON.parse(response.body, symbolize_names: true) expect(user_response).to have_key(:errors) end From 463e725950ec3cc9d93a818415590f72edf23010 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 22 Apr 2015 03:02:23 -0700 Subject: [PATCH 08/10] Tests Pass by not verifying doubles -- problems for Rspec 4 --- spec/controllers/api/v1/projects_controller_spec.rb | 2 +- spec/rails_helper.rb | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/spec/controllers/api/v1/projects_controller_spec.rb b/spec/controllers/api/v1/projects_controller_spec.rb index ec967b6..57ee9ca 100644 --- a/spec/controllers/api/v1/projects_controller_spec.rb +++ b/spec/controllers/api/v1/projects_controller_spec.rb @@ -1,4 +1,4 @@ -require 'rails_helper' +require 'rails_helper' require 'api/v1/projects_controller' diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c755bc2..b4562b5 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,6 +1,6 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' ENV['RAILS_ENV'] ||= 'test' -require 'rails_helper' +require 'spec_helper' require File.expand_path('../../config/environment', __FILE__) require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! @@ -57,4 +57,14 @@ # The different available types are documented in the features, such as in # https://relishapp.com/rspec/rspec-rails/docs config.infer_spec_type_from_file_location! + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = false + end + end From 5ad164bdf61818f4505d3cbaa0a2fce62a725760 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 22 Apr 2015 03:03:14 -0700 Subject: [PATCH 09/10] fix gemfile typo --- Gemfile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 7123037..a773947 100644 --- a/Gemfile +++ b/Gemfile @@ -42,9 +42,7 @@ gem 'rack-cache' group :development, :test do # Cucumber, testing and coverage gem 'cucumber-rails', :require => false - gem 'rspec-rails', '>= 3.1 - - ' + gem 'rspec-rails', '>= 3.1' gem 'simplecov' gem "factory_girl_rails" gem 'faker' From 1e557269c0d1911b59ebc3264d97d5dac9136b70 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 22 Apr 2015 03:16:23 -0700 Subject: [PATCH 10/10] Fix a deprecation warning, only 1 left...but bed time. --- spec/controllers/pages_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controllers/pages_controller_spec.rb b/spec/controllers/pages_controller_spec.rb index f2c3252..c3bb12c 100644 --- a/spec/controllers/pages_controller_spec.rb +++ b/spec/controllers/pages_controller_spec.rb @@ -5,7 +5,7 @@ describe "GET 'index'" do it "returns http success" do get 'index' - response.should be_success + expect(response).to be_success end end