From a93473f2c9a7ab8e210d09c4993ee9e2716e3397 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Wed, 16 Dec 2015 22:33:29 +0800 Subject: [PATCH] rubocop spec folder --- spec/controllers/mappings_controller_spec.rb | 20 +++-- spec/controllers/maps_controller_spec.rb | 20 +++-- spec/controllers/metacodes_controller_spec.rb | 20 +++-- spec/controllers/synapses_controller_spec.rb | 20 +++-- spec/controllers/topics_controller_spec.rb | 44 ++++------ spec/factories/users.rb | 4 +- spec/rails_helper.rb | 6 +- spec/routing/metacodes_routing_spec.rb | 4 - spec/spec_helper.rb | 87 ++----------------- spec/support/controller_helpers.rb | 19 ++-- 10 files changed, 99 insertions(+), 145 deletions(-) diff --git a/spec/controllers/mappings_controller_spec.rb b/spec/controllers/mappings_controller_spec.rb index 1fc3e181..8a7acda5 100644 --- a/spec/controllers/mappings_controller_spec.rb +++ b/spec/controllers/mappings_controller_spec.rb @@ -100,20 +100,26 @@ RSpec.describe MappingsController, type: :controller do it 'updates the requested mapping' do mapping = Mapping.create! valid_attributes - put :update, { id: mapping.to_param, mapping: new_attributes }, valid_session + put :update, + { id: mapping.to_param, mapping: new_attributes }, + valid_session mapping.reload skip('Add assertions for updated state') end it 'assigns the requested mapping as @mapping' do mapping = Mapping.create! valid_attributes - put :update, { id: mapping.to_param, mapping: valid_attributes }, valid_session + put :update, + { id: mapping.to_param, mapping: valid_attributes }, + valid_session expect(assigns(:mapping)).to eq(mapping) end it 'redirects to the mapping' do mapping = Mapping.create! valid_attributes - put :update, { id: mapping.to_param, mapping: valid_attributes }, valid_session + put :update, + { id: mapping.to_param, mapping: valid_attributes }, + valid_session expect(response).to redirect_to(mapping) end end @@ -121,13 +127,17 @@ RSpec.describe MappingsController, type: :controller do context 'with invalid params' do it 'assigns the mapping as @mapping' do mapping = Mapping.create! valid_attributes - put :update, { id: mapping.to_param, mapping: invalid_attributes }, valid_session + put :update, + { id: mapping.to_param, mapping: invalid_attributes }, + valid_session expect(assigns(:mapping)).to eq(mapping) end it "re-renders the 'edit' template" do mapping = Mapping.create! valid_attributes - put :update, { id: mapping.to_param, mapping: invalid_attributes }, valid_session + put :update, + { id: mapping.to_param, mapping: invalid_attributes }, + valid_session expect(response).to render_template('edit') end end diff --git a/spec/controllers/maps_controller_spec.rb b/spec/controllers/maps_controller_spec.rb index ff16c87b..fdaa064a 100644 --- a/spec/controllers/maps_controller_spec.rb +++ b/spec/controllers/maps_controller_spec.rb @@ -100,20 +100,26 @@ RSpec.describe MapsController, type: :controller do it 'updates the requested map' do map = Map.create! valid_attributes - put :update, { id: map.to_param, map: new_attributes }, valid_session + put :update, + { id: map.to_param, map: new_attributes }, + valid_session map.reload skip('Add assertions for updated state') end it 'assigns the requested map as @map' do map = Map.create! valid_attributes - put :update, { id: map.to_param, map: valid_attributes }, valid_session + put :update, + { id: map.to_param, map: valid_attributes }, + valid_session expect(assigns(:map)).to eq(map) end it 'redirects to the map' do map = Map.create! valid_attributes - put :update, { id: map.to_param, map: valid_attributes }, valid_session + put :update, + { id: map.to_param, map: valid_attributes }, + valid_session expect(response).to redirect_to(map) end end @@ -121,13 +127,17 @@ RSpec.describe MapsController, type: :controller do context 'with invalid params' do it 'assigns the map as @map' do map = Map.create! valid_attributes - put :update, { id: map.to_param, map: invalid_attributes }, valid_session + put :update, + { id: map.to_param, map: invalid_attributes }, + valid_session expect(assigns(:map)).to eq(map) end it "re-renders the 'edit' template" do map = Map.create! valid_attributes - put :update, { id: map.to_param, map: invalid_attributes }, valid_session + put :update, + { id: map.to_param, map: invalid_attributes }, + valid_session expect(response).to render_template('edit') end end diff --git a/spec/controllers/metacodes_controller_spec.rb b/spec/controllers/metacodes_controller_spec.rb index 6f307cb6..6e1ba2b9 100644 --- a/spec/controllers/metacodes_controller_spec.rb +++ b/spec/controllers/metacodes_controller_spec.rb @@ -112,20 +112,26 @@ RSpec.describe MetacodesController, type: :controller do it 'updates the requested metacode' do metacode = Metacode.create! valid_attributes - put :update, { id: metacode.to_param, metacode: new_attributes }, valid_session + put :update, + { id: metacode.to_param, metacode: new_attributes }, + valid_session metacode.reload skip('Add assertions for updated state') end it 'assigns the requested metacode as @metacode' do metacode = Metacode.create! valid_attributes - put :update, { id: metacode.to_param, metacode: valid_attributes }, valid_session + put :update, + { id: metacode.to_param, metacode: valid_attributes }, + valid_session expect(assigns(:metacode)).to eq(metacode) end it 'redirects to the metacode' do metacode = Metacode.create! valid_attributes - put :update, { id: metacode.to_param, metacode: valid_attributes }, valid_session + put :update, + { id: metacode.to_param, metacode: valid_attributes }, + valid_session expect(response).to redirect_to(metacode) end end @@ -133,13 +139,17 @@ RSpec.describe MetacodesController, type: :controller do context 'with invalid params' do it 'assigns the metacode as @metacode' do metacode = Metacode.create! valid_attributes - put :update, { id: metacode.to_param, metacode: invalid_attributes }, valid_session + put :update, + { id: metacode.to_param, metacode: invalid_attributes }, + valid_session expect(assigns(:metacode)).to eq(metacode) end it "re-renders the 'edit' template" do metacode = Metacode.create! valid_attributes - put :update, { id: metacode.to_param, metacode: invalid_attributes }, valid_session + put :update, + { id: metacode.to_param, metacode: invalid_attributes }, + valid_session expect(response).to render_template('edit') end end diff --git a/spec/controllers/synapses_controller_spec.rb b/spec/controllers/synapses_controller_spec.rb index 62f9c6ac..ff05ea6f 100644 --- a/spec/controllers/synapses_controller_spec.rb +++ b/spec/controllers/synapses_controller_spec.rb @@ -100,20 +100,26 @@ RSpec.describe SynapsesController, type: :controller do it 'updates the requested synapse' do synapse = Synapse.create! valid_attributes - put :update, { id: synapse.to_param, synapse: new_attributes }, valid_session + put :update, + { id: synapse.to_param, synapse: new_attributes }, + valid_session synapse.reload skip('Add assertions for updated state') end it 'assigns the requested synapse as @synapse' do synapse = Synapse.create! valid_attributes - put :update, { id: synapse.to_param, synapse: valid_attributes }, valid_session + put :update, + { id: synapse.to_param, synapse: valid_attributes }, + valid_session expect(assigns(:synapse)).to eq(synapse) end it 'redirects to the synapse' do synapse = Synapse.create! valid_attributes - put :update, { id: synapse.to_param, synapse: valid_attributes }, valid_session + put :update, + { id: synapse.to_param, synapse: valid_attributes }, + valid_session expect(response).to redirect_to(synapse) end end @@ -121,13 +127,17 @@ RSpec.describe SynapsesController, type: :controller do context 'with invalid params' do it 'assigns the synapse as @synapse' do synapse = Synapse.create! valid_attributes - put :update, { id: synapse.to_param, synapse: invalid_attributes }, valid_session + put :update, + { id: synapse.to_param, synapse: invalid_attributes }, + valid_session expect(assigns(:synapse)).to eq(synapse) end it "re-renders the 'edit' template" do synapse = Synapse.create! valid_attributes - put :update, { id: synapse.to_param, synapse: invalid_attributes }, valid_session + put :update, + { id: synapse.to_param, synapse: invalid_attributes }, + valid_session expect(response).to render_template('edit') end end diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index 8fefd0b2..35b2156e 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -1,27 +1,6 @@ require 'rails_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - RSpec.describe TopicsController, type: :controller do - # This should return the minimal set of attributes required to create a valid - # Topic. As you add validations to Topic, be sure to - # adjust the attributes here as well. let(:valid_attributes) do skip('Add a hash of attributes valid for your model') end @@ -30,9 +9,6 @@ RSpec.describe TopicsController, type: :controller do skip('Add a hash of attributes invalid for your model') end - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # TopicsController. Be sure to keep this updated too. let(:valid_session) { {} } describe 'GET #index' do @@ -100,20 +76,26 @@ RSpec.describe TopicsController, type: :controller do it 'updates the requested topic' do topic = Topic.create! valid_attributes - put :update, { id: topic.to_param, topic: new_attributes }, valid_session + put :update, + { id: topic.to_param, topic: new_attributes }, + valid_session topic.reload skip('Add assertions for updated state') end it 'assigns the requested topic as @topic' do topic = Topic.create! valid_attributes - put :update, { id: topic.to_param, topic: valid_attributes }, valid_session + put :update, + { id: topic.to_param, topic: valid_attributes }, + valid_session expect(assigns(:topic)).to eq(topic) end it 'redirects to the topic' do topic = Topic.create! valid_attributes - put :update, { id: topic.to_param, topic: valid_attributes }, valid_session + put :update, + { id: topic.to_param, topic: valid_attributes }, + valid_session expect(response).to redirect_to(topic) end end @@ -121,13 +103,17 @@ RSpec.describe TopicsController, type: :controller do context 'with invalid params' do it 'assigns the topic as @topic' do topic = Topic.create! valid_attributes - put :update, { id: topic.to_param, topic: invalid_attributes }, valid_session + put :update, + { id: topic.to_param, topic: invalid_attributes }, + valid_session expect(assigns(:topic)).to eq(topic) end it "re-renders the 'edit' template" do topic = Topic.create! valid_attributes - put :update, { id: topic.to_param, topic: invalid_attributes }, valid_session + put :update, + { id: topic.to_param, topic: invalid_attributes }, + valid_session expect(response).to render_template('edit') end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index c645aa6b..8bdcb17e 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -5,6 +5,8 @@ FactoryGirl.define do code { random_string(8) } joinedwithcode { code } password 'omgwtfbbq' - to_create { |instance| instance.save(validate: false) } # bypass validation of the joinedwithcode + + # bypass validation of the joinedwithcode + to_create { |instance| instance.save(validate: false) } end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c109fae8..17c04cd7 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,8 +1,12 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) + # Prevent database truncation if the environment is production -abort('The Rails environment is running in production mode!') if Rails.env.production? +if Rails.env.production? + abort('The Rails environment is running in production mode!') +end + require 'spec_helper' require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! diff --git a/spec/routing/metacodes_routing_spec.rb b/spec/routing/metacodes_routing_spec.rb index 1f129448..151148fe 100644 --- a/spec/routing/metacodes_routing_spec.rb +++ b/spec/routing/metacodes_routing_spec.rb @@ -21,9 +21,5 @@ RSpec.describe MetacodesController, type: :routing do it 'routes to #update via PUT' do expect(put: '/metacodes/1').to route_to('metacodes#update', id: '1') end - - # it "routes to #destroy" do - # expect(:delete => "/metacodes/1").to route_to("metacodes#destroy", :id => "1") - # end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c72432be..14cfd1c7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,96 +1,19 @@ -# 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. - # # 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 - # - # # Allows RSpec to persist some state between runs in order to support - # # the `--only-failures` and `--next-failure` CLI options. We recommend - # # you configure your source control system to ignore this file. - # config.example_status_persistence_file_path = "spec/examples.txt" - # - # # 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://www.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 + # 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 end def random_string(length = 10) o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten - string = (0...length).map { o[rand(o.length)] }.join + (0...length).map { o[rand(o.length)] }.join end diff --git a/spec/support/controller_helpers.rb b/spec/support/controller_helpers.rb index b148ff6e..5fe34854 100644 --- a/spec/support/controller_helpers.rb +++ b/spec/support/controller_helpers.rb @@ -1,14 +1,17 @@ # https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs module ControllerHelpers + # rubocop:disable Metrics/AbcSize def sign_in(user = create(:user)) - if user.nil? - # simulate unauthenticated - allow(request.env['warden']).to receive(:authenticate!).and_throw(:warden, scope: :user) - allow(controller).to receive(:current_user).and_return(nil) - else - # simulate authenticated - allow(request.env['warden']).to receive(:authenticate!).and_return(user) - allow(controller).to receive(:current_user).and_return(user) + if user.nil? # simulate unauthenticated + allow(request.env['warden']).to( + receive(:authenticate!).and_throw(:warden, scope: :user) + ) + else # simulate authenticated + allow(request.env['warden']).to( + receive(:authenticate!).and_return(user) + ) end + allow(controller).to receive(:current_user).and_return(user) end + # rubocop:enable Metrics/AbcSize end