From ef7bec89dc772e511bb2403b12a2131a5b85bd56 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 20 Jul 2020 09:35:41 -0400 Subject: [PATCH] Fix CI builds (#286) * update test app gems and fix more deprecation errors * replace minitest with rspec for consistency. cleanup Rakefile --- .gitignore | 4 +- .rubocop.yml | 24 + .rubocop_todo.yml | 748 ++++++++++++++++++ .travis.yml | 28 +- Gemfile | 13 +- Rakefile | 26 +- app/controllers/impressionist_controller.rb | 25 +- gemfiles/rails32.gemfile | 31 - gemfiles/rails40.gemfile | 31 - impressionist.gemspec | 13 +- .../active_record/impressionist_generator.rb | 2 +- ...ble.rb => create_impressions_table.rb.erb} | 0 lib/generators/impressionist_generator.rb | 2 +- .../{impression.rb => impression.rb.erb} | 0 spec/models/bots_spec.rb | 25 + spec/rails_helper.rb | 11 + spec/rails_toggle_spec.rb | 31 + spec/setup_association_spec.rb | 48 ++ spec/spec_helper.rb | 100 +++ tests/spec/minitest_helper.rb | 7 - tests/spec/rails_toggle_spec.rb | 38 - tests/spec/setup_association_spec.rb | 56 -- tests/test_app/.rspec | 1 + tests/test_app/Gemfile | 20 +- tests/test_app/app/assets/config/manifest.js | 1 + .../app/controllers/dummy_controller.rb | 2 +- .../config/environments/development.rb | 8 +- tests/test_app/config/environments/test.rb | 6 +- .../config/initializers/secret_token.rb | 2 +- .../migrate/20110201153144_create_articles.rb | 2 +- .../db/migrate/20110210205028_create_posts.rb | 2 +- .../migrate/20111127184039_create_widgets.rb | 2 +- ...20130719024021_create_impressions_table.rb | 2 +- .../migrate/20150207135825_create_profiles.rb | 2 +- ...20150207140310_create_friendly_id_slugs.rb | 2 +- tests/test_app/db/schema.rb | 119 ++- .../controllers/articles_controller_spec.rb | 127 +-- .../spec/controllers/dummy_controller_spec.rb | 10 +- .../impressionist_uniqueness_spec.rb | 381 +++++---- .../spec/controllers/posts_controller_spec.rb | 44 +- .../controllers/widgets_controller_spec.rb | 142 ++-- .../spec/initializers/initializers_spec.rb | 13 +- tests/test_app/spec/models/bots_spec.rb | 15 +- .../spec/models/counter_caching_spec.rb | 46 +- tests/test_app/spec/models/model_spec.rb | 60 +- .../rails_generators/rails_generators_spec.rb | 14 +- tests/test_app/spec/spec_helper.rb | 13 +- upgrade_migrations/version_0_3_0.rb | 2 +- upgrade_migrations/version_0_4_0.rb | 2 +- upgrade_migrations/version_1_5_2.rb | 2 +- 50 files changed, 1623 insertions(+), 682 deletions(-) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml delete mode 100644 gemfiles/rails32.gemfile delete mode 100644 gemfiles/rails40.gemfile rename lib/generators/active_record/templates/{create_impressions_table.rb => create_impressions_table.rb.erb} (100%) rename lib/generators/templates/{impression.rb => impression.rb.erb} (100%) create mode 100644 spec/models/bots_spec.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/rails_toggle_spec.rb create mode 100644 spec/setup_association_spec.rb create mode 100644 spec/spec_helper.rb delete mode 100644 tests/spec/minitest_helper.rb delete mode 100644 tests/spec/rails_toggle_spec.rb delete mode 100644 tests/spec/setup_association_spec.rb create mode 100644 tests/test_app/app/assets/config/manifest.js diff --git a/.gitignore b/.gitignore index 66e4aa4..0f5c91e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,7 @@ /test_app/vendor Gemfile.lock gemfiles/*.lock +.DS_Store *.swo -*.swp +.vscode/ + diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..5806485 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,24 @@ +AllCops: + Exclude: + - 'tests/test_app/db/**/*.rb' + +inherit_from: .rubocop_todo.yml + +require: + - rubocop-rspec + - rubocop-rails + +Layout/SpaceAroundMethodCallOperator: + Enabled: true +Lint/RaiseException: + Enabled: True +Lint/StructNewOverride: + Enabled: True +Style/ExponentialNotation: + Enabled: True +Style/HashEachMethods: + Enabled: True +Style/HashTransformKeys: + Enabled: True +Style/HashTransformValues: + Enabled: True diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..2706f35 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,748 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2020-04-25 16:13:40 -0400 using RuboCop version 0.82.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 7 +# Cop supports --auto-correct. +# Configuration parameters: TreatCommentsAsGroupSeparators, Include. +# Include: **/*.gemfile, **/Gemfile, **/gems.rb +Bundler/OrderedGems: + Exclude: + - 'tests/test_app/Gemfile' + +# Offense count: 1 +# Configuration parameters: Include. +# Include: **/*.gemspec +Gemspec/RubyVersionGlobalsUsage: + Exclude: + - 'impressionist.gemspec' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: outdent, indent +Layout/AccessModifierIndentation: + Exclude: + - 'lib/impressionist/engine.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: with_first_argument, with_fixed_indentation +Layout/ArgumentAlignment: + Exclude: + - 'lib/impressionist/is_impressionable.rb' + - 'lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb' + +# Offense count: 1455 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: with_first_element, with_fixed_indentation +Layout/ArrayAlignment: + Exclude: + - 'app/models/impressionist/bots.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleAlignWith. +# SupportedStylesAlignWith: either, start_of_block, start_of_line +Layout/BlockAlignment: + Exclude: + - 'lib/impressionist/engine.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +Layout/ClosingParenthesisIndentation: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'lib/impressionist/controllers/mongoid/impressionist_controller.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +Layout/CommentIndentation: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'tests/test_app/config/initializers/impression.rb' + +# Offense count: 7 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: leading, trailing +Layout/DotPosition: + Exclude: + - 'lib/impressionist/counter_cache.rb' + - 'lib/impressionist/models/active_record/impressionist/impressionable.rb' + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + - 'lib/impressionist/setup_association.rb' + - 'lib/impressionist/update_counters.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Layout/EmptyLineAfterGuardClause: + Exclude: + - 'app/models/impressionist/bots.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowAdjacentOneLineDefs, NumberOfEmptyLines. +Layout/EmptyLineBetweenDefs: + Exclude: + - 'lib/impressionist/counter_cache.rb' + +# Offense count: 8 +# Cop supports --auto-correct. +Layout/EmptyLines: + Exclude: + - 'Rakefile' + - 'app/controllers/impressionist_controller.rb' + - 'lib/impressionist/counter_cache.rb' + - 'lib/impressionist/engine.rb' + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + - 'tests/test_app/Gemfile' + - 'tests/test_app/lib/tasks/cucumber.rake' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: around, only_before +Layout/EmptyLinesAroundAccessModifier: + Exclude: + - 'lib/impressionist/setup_association.rb' + +# Offense count: 7 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines, beginning_only, ending_only +Layout/EmptyLinesAroundClassBody: + Exclude: + - 'lib/generators/impressionist_generator.rb' + - 'lib/impressionist/engine.rb' + - 'lib/impressionist/models/active_record/impression.rb' + - 'lib/impressionist/models/mongoid/impression.rb' + - 'lib/impressionist/rails_toggle.rb' + - 'lib/impressionist/update_counters.rb' + - 'tests/test_app/app/controllers/dummy_controller.rb' + +# Offense count: 7 +# Cop supports --auto-correct. +Layout/EmptyLinesAroundMethodBody: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'lib/impressionist/counter_cache.rb' + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + - 'tests/test_app/app/controllers/posts_controller.rb' + - 'upgrade_migrations/version_0_3_0.rb' + +# Offense count: 19 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines +Layout/EmptyLinesAroundModuleBody: + Exclude: + - 'app/models/impressionist/bots.rb' + - 'app/models/impressionist/impressionable.rb' + - 'lib/impressionist/counter_cache.rb' + - 'lib/impressionist/is_impressionable.rb' + - 'lib/impressionist/models/active_record/impressionist/impressionable.rb' + - 'lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb' + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + - 'lib/impressionist/rails_toggle.rb' + - 'lib/impressionist/update_counters.rb' + +# Offense count: 7 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment. +Layout/ExtraSpacing: + Exclude: + - 'lib/impressionist/counter_cache.rb' + - 'tests/test_app/config.ru' + - 'tests/test_app/config/environments/pg_test.rb' + - 'tests/test_app/config/environments/test.rb' + - 'tests/test_app/script/rails' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: consistent, consistent_relative_to_receiver, special_for_inner_method_call, special_for_inner_method_call_in_parentheses +Layout/FirstArgumentIndentation: + Exclude: + - 'lib/impressionist/controllers/mongoid/impressionist_controller.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. +# SupportedHashRocketStyles: key, separator, table +# SupportedColonStyles: key, separator, table +# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit +Layout/HashAlignment: + Exclude: + - 'lib/impressionist/controllers/mongoid/impressionist_controller.rb' + +# Offense count: 18 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: normal, indented_internal_methods +Layout/IndentationConsistency: + Exclude: + - 'lib/impressionist/counter_cache.rb' + - 'lib/impressionist/engine.rb' + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + - 'lib/impressionist/rails_toggle.rb' + - 'lib/impressionist/setup_association.rb' + +# Offense count: 8 +# Cop supports --auto-correct. +# Configuration parameters: IndentationWidth, EnforcedStyle. +# SupportedStyles: spaces, tabs +Layout/IndentationStyle: + Exclude: + - 'lib/impressionist/controllers/mongoid/impressionist_controller.rb' + +# Offense count: 25 +# Cop supports --auto-correct. +# Configuration parameters: Width, IgnoredPatterns. +Layout/IndentationWidth: + Exclude: + - 'app/models/impressionist/impressionable.rb' + - 'lib/impressionist/controllers/mongoid/impressionist_controller.rb' + - 'lib/impressionist/counter_cache.rb' + - 'lib/impressionist/engine.rb' + - 'lib/impressionist/models/active_record/impressionist/impressionable.rb' + - 'lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb' + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + - 'lib/impressionist/rails_toggle.rb' + - 'lib/impressionist/setup_association.rb' + - 'tests/test_app/lib/tasks/cucumber.rake' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: AllowDoxygenCommentStyle, AllowGemfileRubyComment. +Layout/LeadingCommentSpace: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'lib/impressionist/bots.rb' + - 'tests/test_app/config/initializers/impression.rb' + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: aligned, indented, indented_relative_to_receiver +Layout/MultilineMethodCallIndentation: + Exclude: + - 'lib/impressionist/counter_cache.rb' + - 'lib/impressionist/models/active_record/impressionist/impressionable.rb' + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + - 'lib/impressionist/setup_association.rb' + - 'lib/impressionist/update_counters.rb' + +# Offense count: 22 +# Cop supports --auto-correct. +Layout/SpaceAfterComma: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'app/models/impressionist/bots.rb' + - 'lib/impressionist/bots.rb' + - 'tests/test_app/app/controllers/articles_controller.rb' + - 'upgrade_migrations/version_0_3_0.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleInsidePipes. +# SupportedStylesInsidePipes: space, no_space +Layout/SpaceAroundBlockParameters: + Exclude: + - 'app/controllers/impressionist_controller.rb' + +# Offense count: 13 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, no_space +Layout/SpaceAroundEqualsInParameterDefault: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'app/models/impressionist/impressionable.rb' + - 'lib/impressionist/controllers/mongoid/impressionist_controller.rb' + - 'lib/impressionist/counter_cache.rb' + - 'lib/impressionist/is_impressionable.rb' + - 'lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb' + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + +# Offense count: 8 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator. +# SupportedStylesForExponentOperator: space, no_space +Layout/SpaceAroundOperators: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'lib/impressionist/controllers/mongoid/impressionist_controller.rb' + - 'lib/impressionist/counter_cache.rb' + - 'lib/impressionist/models/mongo_mapper/impression.rb' + - 'tests/test_app/app/controllers/widgets_controller.rb' + - 'tests/test_app/config/environments/pg_test.rb' + - 'tests/test_app/config/environments/test.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. +# SupportedStyles: space, no_space +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceBeforeBlockBraces: + Exclude: + - 'app/controllers/impressionist_controller.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. +# SupportedStyles: space, no_space +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceInsideBlockBraces: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'tests/test_app/lib/tasks/cucumber.rake' + +# Offense count: 10 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. +# SupportedStyles: space, no_space, compact +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceInsideHashLiteralBraces: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'lib/impressionist/update_counters.rb' + - 'tests/test_app/lib/tasks/cucumber.rake' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: final_newline, final_blank_line +Layout/TrailingEmptyLines: + Exclude: + - 'Gemfile' + - 'lib/impressionist/setup_association.rb' + - 'tests/test_app/config/initializers/impression.rb' + - 'upgrade_migrations/version_1_5_2.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowInHeredoc. +Layout/TrailingWhitespace: + Exclude: + - 'lib/impressionist/setup_association.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Lint/DeprecatedClassMethods: + Exclude: + - 'tests/test_app/config/boot.rb' + +# Offense count: 1 +Lint/DuplicateMethods: + Exclude: + - 'lib/impressionist/setup_association.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Lint/RedundantStringCoercion: + Exclude: + - 'app/controllers/impressionist_controller.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Lint/ScriptPermission: + Exclude: + - 'tests/test_app/Rakefile' + +# Offense count: 2 +# Cop supports --auto-correct. +Lint/SendWithMixinArgument: + Exclude: + - 'lib/impressionist/models/active_record/impressionist/impressionable.rb' + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments. +Lint/UnusedBlockArgument: + Exclude: + - 'lib/impressionist/engine.rb' + +# Offense count: 3 +# Configuration parameters: IgnoredMethods. +Metrics/AbcSize: + Max: 23 + +# Offense count: 10 +# Configuration parameters: CountComments, ExcludedMethods. +# ExcludedMethods: refine +Metrics/BlockLength: + Max: 336 + +# Offense count: 3 +# Configuration parameters: CountComments, ExcludedMethods. +Metrics/MethodLength: + Max: 15 + +# Offense count: 2 +# Configuration parameters: CountComments. +Metrics/ModuleLength: + Max: 1462 + +# Offense count: 2 +# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros. +# NamePrefix: is_, has_, have_ +# ForbiddenPrefixes: is_, has_, have_ +# AllowedMethods: is_a? +# MethodDefinitionMacros: define_method, define_singleton_method +Naming/PredicateName: + Exclude: + - 'spec/**/*' + - 'lib/impressionist/is_impressionable.rb' + - 'lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb' + +# Offense count: 6 +# Configuration parameters: Prefixes. +# Prefixes: when, with, without +RSpec/ContextWording: + Exclude: + - 'tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb' + - 'tests/test_app/spec/controllers/widgets_controller_spec.rb' + +# Offense count: 31 +# Configuration parameters: Max. +RSpec/ExampleLength: + Exclude: + - 'spec/setup_association_spec.rb' + - 'tests/test_app/spec/controllers/articles_controller_spec.rb' + - 'tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb' + - 'tests/test_app/spec/controllers/posts_controller_spec.rb' + - 'tests/test_app/spec/controllers/widgets_controller_spec.rb' + - 'tests/test_app/spec/rails_generators/rails_generators_spec.rb' + +# Offense count: 5 +# Configuration parameters: CustomTransform, IgnoreMethods. +RSpec/FilePath: + Exclude: + - 'spec/models/bots_spec.rb' + - 'spec/rails_toggle_spec.rb' + - 'spec/setup_association_spec.rb' + - 'tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb' + - 'tests/test_app/spec/models/bots_spec.rb' + +# Offense count: 61 +# Configuration parameters: AssignmentOnly. +RSpec/InstanceVariable: + Exclude: + - 'tests/test_app/spec/controllers/articles_controller_spec.rb' + - 'tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb' + - 'tests/test_app/spec/controllers/widgets_controller_spec.rb' + - 'tests/test_app/spec/models/model_spec.rb' + +# Offense count: 5 +# Configuration parameters: . +# SupportedStyles: have_received, receive +RSpec/MessageSpies: + EnforcedStyle: receive + +# Offense count: 28 +# Configuration parameters: AggregateFailuresByDefault. +RSpec/MultipleExpectations: + Max: 8 + +# Offense count: 4 +RSpec/NestedGroups: + Max: 4 + +# Offense count: 2 +RSpec/RepeatedExample: + Exclude: + - 'tests/test_app/spec/controllers/widgets_controller_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, Include. +# SupportedStyles: action, filter +# Include: app/controllers/**/*.rb +Rails/ActionFilter: + Exclude: + - 'app/controllers/impressionist_controller.rb' + +# Offense count: 6 +# Cop supports --auto-correct. +Rails/ApplicationRecord: + Exclude: + - 'lib/impressionist/models/active_record/impression.rb' + - 'tests/test_app/app/models/article.rb' + - 'tests/test_app/app/models/dummy.rb' + - 'tests/test_app/app/models/post.rb' + - 'tests/test_app/app/models/profile.rb' + - 'tests/test_app/app/models/widget.rb' + +# Offense count: 2 +# Configuration parameters: EnforcedStyle. +# SupportedStyles: slashes, arguments +Rails/FilePath: + Exclude: + - 'tests/test_app/lib/tasks/cucumber.rake' + - 'tests/test_app/spec/rails_generators/rails_generators_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: NotNilAndNotEmpty, NotBlank, UnlessBlank. +Rails/Present: + Exclude: + - 'app/controllers/impressionist_controller.rb' + +# Offense count: 2 +# Configuration parameters: Include. +# Include: **/Rakefile, **/*.rake +Rails/RakeEnvironment: + Exclude: + - 'lib/capistrano/tasks/**/*.rake' + - 'Rakefile' + - 'tests/test_app/lib/tasks/cucumber.rake' + +# Offense count: 1 +# Configuration parameters: Blacklist, Whitelist. +# Blacklist: decrement!, decrement_counter, increment!, increment_counter, toggle!, touch, update_all, update_attribute, update_column, update_columns, update_counters +Rails/SkipsModelValidations: + Exclude: + - 'lib/impressionist/update_counters.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: strict, flexible +Rails/TimeZone: + Exclude: + - 'app/models/impressionist/impressionable.rb' + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/BlockComments: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods, AllowBracesOnProceduralOneLiners, BracesRequiredMethods. +# SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces +# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object +# FunctionalMethods: let, let!, subject, watch +# IgnoredMethods: lambda, proc, it +Style/BlockDelimiters: + Exclude: + - 'lib/impressionist/counter_cache.rb' + +# Offense count: 1 +Style/ClassVars: + Exclude: + - 'lib/impressionist.rb' + +# Offense count: 1 +Style/CommentedKeyword: + Exclude: + - 'app/models/impressionist/impressionable.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SingleLineConditionsOnly, IncludeTernaryExpressions. +# SupportedStyles: assign_to_condition, assign_inside_condition +Style/ConditionalAssignment: + Exclude: + - 'app/controllers/impressionist_controller.rb' + +# Offense count: 36 +Style/Documentation: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +Style/EachWithObject: + Exclude: + - 'app/controllers/impressionist_controller.rb' + +# Offense count: 8 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: compact, expanded +Style/EmptyMethod: + Exclude: + - 'tests/test_app/app/controllers/dummy_controller.rb' + - 'tests/test_app/app/controllers/posts_controller.rb' + - 'tests/test_app/app/controllers/profiles_controller.rb' + - 'tests/test_app/app/controllers/widgets_controller.rb' + +# Offense count: 7 +# Cop supports --auto-correct. +Style/ExpandPathArguments: + Exclude: + - 'lib/generators/impressionist_generator.rb' + - 'tests/test_app/Rakefile' + - 'tests/test_app/config/application.rb' + - 'tests/test_app/config/boot.rb' + - 'tests/test_app/config/environment.rb' + - 'tests/test_app/script/rails' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: format, sprintf, percent +Style/FormatString: + Exclude: + - 'lib/generators/active_record/impressionist_generator.rb' + +# Offense count: 1 +# Configuration parameters: EnforcedStyle. +# SupportedStyles: annotated, template, unannotated +Style/FormatStringToken: + Exclude: + - 'lib/generators/active_record/impressionist_generator.rb' + +# Offense count: 76 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, always_true, never +Style/FrozenStringLiteralComment: + Enabled: false + +# Offense count: 6 +# Configuration parameters: MinBodyLength. +Style/GuardClause: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'tests/test_app/app/controllers/articles_controller.rb' + - 'tests/test_app/app/controllers/posts_controller.rb' + - 'tests/test_app/app/controllers/profiles_controller.rb' + +# Offense count: 134 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. +# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys +Style/HashSyntax: + Enabled: false + +# Offense count: 4 +# Cop supports --auto-correct. +Style/IfUnlessModifier: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'app/models/impressionist/impressionable.rb' + - 'impressionist.gemspec' + +# Offense count: 1 +Style/MultilineTernaryOperator: + Exclude: + - 'lib/impressionist/models/mongoid/impressionist/impressionable.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: literals, strict +Style/MutableConstant: + Exclude: + - 'app/models/impressionist/bots.rb' + - 'app/models/impressionist/impressionable.rb' + - 'lib/impressionist/bots.rb' + - 'lib/impressionist/version.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/Not: + Exclude: + - 'lib/impressionist/rails_toggle.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: Strict. +Style/NumericLiterals: + MinDigits: 6 + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: PreferredDelimiters. +Style/PercentLiteralDelimiters: + Exclude: + - 'tests/test_app/config/application.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/RedundantBegin: + Exclude: + - 'lib/impressionist/counter_cache.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: AllowMultipleReturnValues. +Style/RedundantReturn: + Exclude: + - 'app/controllers/impressionist_controller.rb' + +# Offense count: 4 +# Cop supports --auto-correct. +Style/RedundantSelf: + Exclude: + - 'app/controllers/impressionist_controller.rb' + - 'lib/impressionist/counter_cache.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +Style/RescueModifier: + Exclude: + - 'app/controllers/impressionist_controller.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/StderrPuts: + Exclude: + - 'tests/test_app/lib/tasks/cucumber.rake' + +# Offense count: 1647 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Enabled: false + +# Offense count: 43 +# Cop supports --auto-correct. +# Configuration parameters: MinSize. +# SupportedStyles: percent, brackets +Style/SymbolArray: + EnforcedStyle: brackets + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: IgnoredMethods. +# IgnoredMethods: respond_to, define_method +Style/SymbolProc: + Exclude: + - 'app/controllers/impressionist_controller.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, AllowSafeAssignment. +# SupportedStyles: require_parentheses, require_no_parentheses, require_parentheses_when_complex +Style/TernaryParentheses: + Exclude: + - 'lib/impressionist/controllers/mongoid/impressionist_controller.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, MinSize, WordRegex. +# SupportedStyles: percent, brackets +Style/WordArray: + Exclude: + - 'app/models/impressionist/bots.rb' + - 'lib/impressionist/bots.rb' + +# Offense count: 433 +# Cop supports --auto-correct. +# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Layout/LineLength: + Max: 197 diff --git a/.travis.yml b/.travis.yml index 1166067..01723fa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,29 +1,29 @@ language: ruby -before_install: - - gem install bundler +env: + - RUBYOPT='-W:no-deprecated -W:no-experimental' before_script: + - bundle - cd tests/test_app + - bundle --gemfile=./Gemfile - bundle exec rails g impressionist -f - bundle exec rake db:create db:migrate RAILS_ENV=test - - cd .. + - cd ../.. script: - - bundle exec rake + - rubocop + - bundle exec rspec + - cd tests/test_app + - export BUNDLE_GEMFILE=./Gemfile + - bundle + - bundle exec rspec rvm: - - 1.9.3 - - 2.0.0 - - 2.1.0 - - 2.2.0 - - 2.3.0 + - 2.7.0 - jruby-head - - rbx + - rbx-4 - ruby-head -gemfile: - - gemfiles/rails32.gemfile - - gemfiles/rails40.gemfile matrix: allow_failures: - rvm: jruby-head - - rvm: rbx + - rvm: rbx-4 - rvm: ruby-head fast_finish: true notifications: diff --git a/Gemfile b/Gemfile index 37823f5..69d002c 100644 --- a/Gemfile +++ b/Gemfile @@ -9,18 +9,13 @@ platforms :jruby do gem 'jruby-openssl' end -platforms :ruby, :mswin, :mingw do - gem 'sqlite3' -end - group :test do - gem 'capybara', '>= 2.0.3' gem 'minitest' - gem 'minitest-rails' - gem 'rails', '>= 3.2.15' - gem 'rspec-rails' + gem 'rspec', "~> 3.0" + gem 'rubocop-rails', require: false + gem 'rubocop-rspec', require: false gem 'simplecov' gem 'systemu' end -gemspec +gemspec \ No newline at end of file diff --git a/Rakefile b/Rakefile index c638c0b..a53e6a1 100644 --- a/Rakefile +++ b/Rakefile @@ -1,18 +1,15 @@ require 'bundler/setup' + +require 'rspec' require 'rspec/core/rake_task' require 'rake/testtask' + +RSpec::Core::RakeTask.new(:spec) +task default: :spec + Bundler::GemHelper.install_tasks -# Impressionist will use MiniTest instead of RSpec -RSpec::Core::RakeTask.new do |task| - task.rspec_opts = "-I ./tests/test_app/spec" - task.pattern = "./tests/test_app/spec/**/*_spec.rb" -end - -task :test_app => :spec -task :default => [:test, :test_app] - namespace :impressionist do require File.dirname(__FILE__) + "/lib/impressionist/bots" @@ -20,15 +17,4 @@ namespace :impressionist do task :bots do p Impressionist::Bots.consume end - -end - -# setup :test task to minitest -# Rake libs default is lib -# libs << path to load test_helper, etc.. -Rake::TestTask.new do |t| - t.libs << "tests/spec" - t.pattern = "tests/spec/**/*_spec.rb" - t.test_files = FileList["tests/spec/**/*_spec.rb"] - t.verbose = true end diff --git a/app/controllers/impressionist_controller.rb b/app/controllers/impressionist_controller.rb index b515623..4c77bac 100644 --- a/app/controllers/impressionist_controller.rb +++ b/app/controllers/impressionist_controller.rb @@ -1,5 +1,6 @@ require 'digest/sha2' + module ImpressionistController module ClassMethods def impressionist(opts={}) @@ -51,7 +52,14 @@ module ImpressionistController # creates a statment hash that contains default values for creating an impression via an AR relation. def associative_create_statement(query_params={}) - filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters) + # support older versions of rails: + # see https://github.com/rails/rails/pull/34039 + if Rails::VERSION::MAJOR < 6 + filter = ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters) + else + filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters) + end + query_params.reverse_merge!( :controller_name => controller_name, :action_name => action_name, @@ -133,14 +141,25 @@ module ImpressionistController end def session_hash + # # careful: request.session_options[:id] encoding in rspec test was ASCII-8BIT # # that broke the database query for uniqueness. not sure if this is a testing only issue. # str = request.session_options[:id] # logger.debug "Encoding: #{str.encoding.inspect}" # # request.session_options[:id].encode("ISO-8859-1") - id = request.session_options[:id] + if Rails::VERSION::MAJOR >= 4 + session["init"] = true + id = session.id.to_s + else + id = request.session_options[:id] + end + + unless id.is_a? String + id = id.cookie_value if Rack::Session::SessionId.const_defined?(:ID_VERSION) && Rack::Session::SessionId::ID_VERSION == 2 + end + + # id = cookies.session.id # rack 2.0.8 releases new version of session id, id.to_s will raise error! - id = id.cookie_value if Rack::Session::SessionId.const_defined?(:ID_VERSION) && Rack::Session::SessionId::ID_VERSION == 2 id end diff --git a/gemfiles/rails32.gemfile b/gemfiles/rails32.gemfile deleted file mode 100644 index 7b74c3a..0000000 --- a/gemfiles/rails32.gemfile +++ /dev/null @@ -1,31 +0,0 @@ -source 'https://rubygems.org' - -gem 'rake', '>= 0.9', '< 11.0' -gem 'rdoc', '>= 2.4.2' - -platforms :jruby do - gem 'activerecord-jdbcsqlite3-adapter' - gem 'jdbc-sqlite3' - gem 'jruby-openssl' -end - -platforms :ruby, :mswin, :mingw do - gem 'sqlite3' -end - -group :test do - gem 'public_suffix', '< 1.5.0' - gem 'capybara', '>= 2.0.3' - gem 'minitest' - gem 'minitest-rails' - gem 'rails', '~> 3.2.15' - gem 'rspec-rails', '~> 2.14.0' - gem 'simplecov' - gem 'systemu' - gem 'friendly_id', '~> 4.0.9' - - # test/unit has been removed by default in Ruby 2.2.x+ - gem 'test-unit' -end - -gemspec :path => '../' diff --git a/gemfiles/rails40.gemfile b/gemfiles/rails40.gemfile deleted file mode 100644 index 465746e..0000000 --- a/gemfiles/rails40.gemfile +++ /dev/null @@ -1,31 +0,0 @@ -source 'https://rubygems.org' - -gem 'rake', '>= 0.9', '< 11.0' -gem 'rdoc', '>= 2.4.2' - -platforms :jruby do - gem 'activerecord-jdbcsqlite3-adapter' - gem 'jdbc-sqlite3' - gem 'jruby-openssl' -end - -platforms :ruby, :mswin, :mingw do - gem 'sqlite3' -end - -group :test do - gem 'public_suffix', '< 1.5.0' - gem 'capybara', '>= 2.0.3' - gem 'minitest' - gem 'minitest-rails' - gem 'rails', '~> 4.0.1' - gem 'rspec-rails', '~> 2.14.0' - gem 'simplecov' - gem 'systemu' - gem 'friendly_id', '~> 5.1.0' - - # test/unit has been removed by default in Ruby 2.2.x+ - gem 'test-unit' -end - -gemspec :path => '../' diff --git a/impressionist.gemspec b/impressionist.gemspec index 3fd6809..b0c95e7 100644 --- a/impressionist.gemspec +++ b/impressionist.gemspec @@ -1,5 +1,6 @@ -# -*- encoding: utf-8 -*- -$:.push File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +$LOAD_PATH.push File.expand_path('lib', __dir__) require 'impressionist/version' Gem::Specification.new do |s| @@ -16,8 +17,12 @@ Gem::Specification.new do |s| s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- tests/**/*`.split("\n") s.require_path = 'lib' - s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6') if s.respond_to? :required_rubygems_version= + if s.respond_to? :required_rubygems_version= + s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6') + end s.add_dependency 'nokogiri', RUBY_VERSION < '2.1.0' ? '~> 1.6.0' : '~> 1' - s.add_development_dependency 'bundler', '~> 1.0' + s.add_dependency 'rails', '>= 3.2.15' + s.add_development_dependency 'bundler', '~> 2.0' + s.add_development_dependency 'sqlite3', '~> 1.4' end diff --git a/lib/generators/active_record/impressionist_generator.rb b/lib/generators/active_record/impressionist_generator.rb index f71a3ab..1c89978 100644 --- a/lib/generators/active_record/impressionist_generator.rb +++ b/lib/generators/active_record/impressionist_generator.rb @@ -15,7 +15,7 @@ module ActiveRecord end def create_migration_file - migration_template 'create_impressions_table.rb', 'db/migrate/create_impressions_table.rb' + migration_template 'create_impressions_table.rb.erb', 'db/migrate/create_impressions_table.rb' end end end diff --git a/lib/generators/active_record/templates/create_impressions_table.rb b/lib/generators/active_record/templates/create_impressions_table.rb.erb similarity index 100% rename from lib/generators/active_record/templates/create_impressions_table.rb rename to lib/generators/active_record/templates/create_impressions_table.rb.erb diff --git a/lib/generators/impressionist_generator.rb b/lib/generators/impressionist_generator.rb index 008f1ec..274a8f0 100644 --- a/lib/generators/impressionist_generator.rb +++ b/lib/generators/impressionist_generator.rb @@ -5,7 +5,7 @@ module Impressionist source_root File.expand_path('../templates', __FILE__) def copy_config_file - template 'impression.rb', 'config/initializers/impression.rb' + template 'impression.rb.erb', 'config/initializers/impression.rb' end end diff --git a/lib/generators/templates/impression.rb b/lib/generators/templates/impression.rb.erb similarity index 100% rename from lib/generators/templates/impression.rb rename to lib/generators/templates/impression.rb.erb diff --git a/spec/models/bots_spec.rb b/spec/models/bots_spec.rb new file mode 100644 index 0000000..26a567a --- /dev/null +++ b/spec/models/bots_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +require './app/models/impressionist/bots' + +describe Impressionist::Bots do + describe "bot detection" do + it "matches wild card" do + expect(described_class).to be_bot("google.com bot") + end + + it "matches a bot list" do + expect(described_class).to be_bot("A-Online Search") + end + + it "skips blank user agents" do + expect(described_class).not_to be_bot + expect(described_class).not_to be_bot("") + expect(described_class).not_to be_bot(nil) + end + + it "skips safe matches" do + expect(described_class).not_to be_bot('127.0.0.1') + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..2318b79 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +ENV['RAILS_ENV'] ||= 'test' + +require 'rails/all' +require 'rspec/rails' + +require File.expand_path('./test_app/config/environment', __dir__) + +RSpec.configure do |config| +end diff --git a/spec/rails_toggle_spec.rb b/spec/rails_toggle_spec.rb new file mode 100644 index 0000000..d2c1293 --- /dev/null +++ b/spec/rails_toggle_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'impressionist/rails_toggle' + +describe Impressionist::RailsToggle do + let(:toggle) { described_class.new } + + context 'when using rails < 4' do + it 'will be included' do + stub_const('::Rails::VERSION::MAJOR', 3) + + expect(toggle).to be_should_include + end + + it 'will not be included when strong parameters is defined' do + stub_const('::Rails::VERSION::MAJOR', 3) + stub_const('StrongParameters', Module.new) + + expect(toggle).not_to be_should_include + end + end + + context 'when using rails >= 4' do + it 'will not be included' do + stub_const('::Rails::VERSION::MAJOR', 4) + + expect(toggle).not_to be_should_include + end + end +end diff --git a/spec/setup_association_spec.rb b/spec/setup_association_spec.rb new file mode 100644 index 0000000..f3ac9b1 --- /dev/null +++ b/spec/setup_association_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: false + +require 'spec_helper' +require 'impressionist/setup_association' +require 'impressionist/rails_toggle' + +describe Impressionist::SetupAssociation do + let(:mock) { double } + let(:setup_association) { described_class.new(mock) } + + it 'will include when togglable' do + expect(mock).to receive(:attr_accessible).with(any_args).and_return(true) + expect(setup_association).to receive(:toggle).and_return(true) + + expect(setup_association).to be_include_attr_acc + end + + it 'will not include if it is not togglable' do + expect(setup_association).to receive(:toggle).and_return(false) + expect(setup_association).not_to be_include_attr_acc + end + + context 'when using rails >= 5' do + it 'sets belongs_to' do + stub_const('::Rails::VERSION::MAJOR', 5) + + expect(mock).to receive(:belongs_to).twice.with( + :impressionable, { polymorphic: true, optional: true } + ).and_return(true) + + expect(setup_association.define_belongs_to).to be_truthy + expect(setup_association.set).to be_falsy + end + end + + context 'when using rails < 5' do + it 'sets belongs_to' do + stub_const('::Rails::VERSION::MAJOR', 4) + + expect(mock).to receive(:belongs_to).twice.with( + :impressionable, { polymorphic: true } + ).and_return(true) + + expect(setup_association.define_belongs_to).to be_truthy + expect(setup_association.set).to be_falsy + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..251aa51 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,100 @@ +# This file was generated by the `rspec --init` 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. +# +# 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 + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows 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. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # 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://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # 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 diff --git a/tests/spec/minitest_helper.rb b/tests/spec/minitest_helper.rb deleted file mode 100644 index e4f904f..0000000 --- a/tests/spec/minitest_helper.rb +++ /dev/null @@ -1,7 +0,0 @@ -$:.unshift(File.dirname __FILE__) - -require 'rails/all' -require 'rails/test_help' -require "minitest/autorun" -require "minitest/pride" -require 'minitest/rails' diff --git a/tests/spec/rails_toggle_spec.rb b/tests/spec/rails_toggle_spec.rb deleted file mode 100644 index 27243d6..0000000 --- a/tests/spec/rails_toggle_spec.rb +++ /dev/null @@ -1,38 +0,0 @@ -# Responsability - # Test whether rails version > 4 - # includes attr_accessible if < 4 -require 'minitest_helper' -require 'impressionist/rails_toggle' - -module Impressionist - describe RailsToggle do - - before { - @toggle = RailsToggle.new - } - - describe "Rails 4" do - - # see your_minitest_path/lib/minitest/mock.rb - it "must not include attr_accessible" do - @toggle.stub :supported_by_rails?, false do - refute @toggle.should_include? - end - end - - end - - describe "Strong Parameters" do - - # see your_minitest_path/lib/minitest/mock.rb - it "must not include attr_accessible" do - @toggle.stub :supported_by_rails?, true do - @toggle.stub :using_strong_parameters?, true do - refute @toggle.should_include? - end - end - end - - end - end -end diff --git a/tests/spec/setup_association_spec.rb b/tests/spec/setup_association_spec.rb deleted file mode 100644 index 644ddc3..0000000 --- a/tests/spec/setup_association_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -require 'minitest_helper' -require 'impressionist/setup_association' - -module Impressionist - describe SetupAssociation do - - let(:mock) { Minitest::Mock.new } - let(:set_up) { SetupAssociation.new(mock) } - - before do - # expects attr_accessible to return true - # and pass 12 arguments - mock. - expect(:attr_accessible, true) do |args| - args.size == 12 - end - - end - - describe "attr_accessible" do - - it "includes" do - set_up.stub :toggle, true do - set_up.include_attr_acc?.must_equal true - - mock.verify - end - end - - end - - describe "belongs_to" do - - it "active_record" do - mock.expect(:belongs_to, true, [Symbol, Hash]) - set_up.define_belongs_to.must_equal true - end - - end - - describe "#set" do - - it "sets an association" do - def set_up.include_attr_acc?; true; end - - set_up.stub(:define_belongs_to, true) { - set_up.set.must_equal true - } - - end - - end - # returns done if thruthy - - end -end diff --git a/tests/test_app/.rspec b/tests/test_app/.rspec index 4e1e0d2..83e16f8 100644 --- a/tests/test_app/.rspec +++ b/tests/test_app/.rspec @@ -1 +1,2 @@ --color +--require spec_helper diff --git a/tests/test_app/Gemfile b/tests/test_app/Gemfile index 06433ab..6e697b4 100644 --- a/tests/test_app/Gemfile +++ b/tests/test_app/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' -gem 'rails', '~> 3.2' -gem 'rake', '< 11.0' +gem 'rails', '~> 6' + gem 'public_suffix', '< 1.5.0' gem 'impressionist', :path => '../../' @@ -12,11 +12,12 @@ platforms :jruby do gem 'jruby-openssl' end +gem 'bigdecimal', '1.4.2' + platforms :ruby, :mswin, :mingw do gem 'pg' - gem 'sqlite3' gem 'mysql2' - gem 'mongoid', '~> 3.1' + gem 'mongoid', '~> 7.0' gem 'bson_ext' end @@ -25,8 +26,8 @@ gem 'json' # Gems used only for assets and not required # in production environments by default. group :assets do - gem 'sass-rails', '~> 3.2' - gem 'coffee-rails', '~> 3.2' + gem 'sass-rails', '~> 5.0' + gem 'coffee-rails', '~> 5.0' # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer' @@ -34,9 +35,11 @@ group :assets do gem 'uglifier', '>= 1.0.3' end + group :development, :test do gem 'autotest-notification' - gem 'rspec-rails', '~> 2.14.0' + gem 'sqlite3', '~> 1.4' + gem 'rspec-rails', '~> 4.0' gem 'spork' # test/unit has been removed by default in Ruby 2.2.x+ @@ -47,7 +50,8 @@ group :test do gem 'capybara' gem 'simplecov' gem 'systemu' + gem 'pry' end gem 'jquery-rails' -gem 'friendly_id', '~> 4.0.9' +gem 'friendly_id', '~> 5' diff --git a/tests/test_app/app/assets/config/manifest.js b/tests/test_app/app/assets/config/manifest.js new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/test_app/app/assets/config/manifest.js @@ -0,0 +1 @@ +{} diff --git a/tests/test_app/app/controllers/dummy_controller.rb b/tests/test_app/app/controllers/dummy_controller.rb index c90cd9c..220fe5b 100644 --- a/tests/test_app/app/controllers/dummy_controller.rb +++ b/tests/test_app/app/controllers/dummy_controller.rb @@ -1,5 +1,5 @@ # This controller imports the impressionist module to make the modules methods available for testing -class DummyController < ActionController::Base +class DummyController < ApplicationController impressionist diff --git a/tests/test_app/config/environments/development.rb b/tests/test_app/config/environments/development.rb index b5306b3..66d7243 100644 --- a/tests/test_app/config/environments/development.rb +++ b/tests/test_app/config/environments/development.rb @@ -22,12 +22,10 @@ TestApp::Application.configure do # Only use best-standards-support built into browsers config.action_dispatch.best_standards_support = :builtin - # Raise exception on mass assignment protection for Active Record models - config.active_record.mass_assignment_sanitizer = :strict + config.eager_load = false - # Log the query plan for queries taking more than this (works - # with SQLite, MySQL, and PostgreSQL) - config.active_record.auto_explain_threshold_in_seconds = 0.5 + # Raise exception on mass assignment protection for Active Record models + # config.active_record.mass_assignment_sanitizer = :strict # Do not compress assets config.assets.compress = false diff --git a/tests/test_app/config/environments/test.rb b/tests/test_app/config/environments/test.rb index 21ec8e4..aba3e9c 100644 --- a/tests/test_app/config/environments/test.rb +++ b/tests/test_app/config/environments/test.rb @@ -29,8 +29,12 @@ TestApp::Application.configure do # ActionMailer::Base.deliveries array. config.action_mailer.delivery_method = :test + config.eager_load = false + + # config.active_record.sqlite3.represent_boolean_as_integer = true + # Raise exception on mass assignment protection for Active Record models - config.active_record.mass_assignment_sanitizer = :strict + # config.active_record.mass_assignment_sanitizer = :strict # Print deprecation notices to the stderr config.active_support.deprecation = :stderr diff --git a/tests/test_app/config/initializers/secret_token.rb b/tests/test_app/config/initializers/secret_token.rb index 30f90d4..cc43d57 100644 --- a/tests/test_app/config/initializers/secret_token.rb +++ b/tests/test_app/config/initializers/secret_token.rb @@ -4,4 +4,4 @@ # If you change this key, all old signed cookies will become invalid! # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. -TestApp::Application.config.secret_token = '4a6fd2eb397985331d209be32073259ed7c25aef4fafcabb00e483ee548e592322277eb15459bdb257b65f31146eda92684b3e7a98ea1b2dfad9b0d08ab62e10' +TestApp::Application.config.secret_key_base = '4a6fd2eb397985331d209be32073259ed7c25aef4fafcabb00e483ee548e592322277eb15459bdb257b65f31146eda92684b3e7a98ea1b2dfad9b0d08ab62e10' diff --git a/tests/test_app/db/migrate/20110201153144_create_articles.rb b/tests/test_app/db/migrate/20110201153144_create_articles.rb index 66f46ef..55c8c71 100644 --- a/tests/test_app/db/migrate/20110201153144_create_articles.rb +++ b/tests/test_app/db/migrate/20110201153144_create_articles.rb @@ -1,4 +1,4 @@ -class CreateArticles < ActiveRecord::Migration +class CreateArticles < ActiveRecord::Migration[4.2] def self.up create_table :articles do |t| t.string :name diff --git a/tests/test_app/db/migrate/20110210205028_create_posts.rb b/tests/test_app/db/migrate/20110210205028_create_posts.rb index f018e1c..3118337 100644 --- a/tests/test_app/db/migrate/20110210205028_create_posts.rb +++ b/tests/test_app/db/migrate/20110210205028_create_posts.rb @@ -1,4 +1,4 @@ -class CreatePosts < ActiveRecord::Migration +class CreatePosts < ActiveRecord::Migration[4.2] def self.up create_table :posts do |t| t.string :name diff --git a/tests/test_app/db/migrate/20111127184039_create_widgets.rb b/tests/test_app/db/migrate/20111127184039_create_widgets.rb index 3305dbc..f64bb27 100644 --- a/tests/test_app/db/migrate/20111127184039_create_widgets.rb +++ b/tests/test_app/db/migrate/20111127184039_create_widgets.rb @@ -1,4 +1,4 @@ -class CreateWidgets < ActiveRecord::Migration +class CreateWidgets < ActiveRecord::Migration[4.2] def self.up create_table :widgets do |t| t.string :name diff --git a/tests/test_app/db/migrate/20130719024021_create_impressions_table.rb b/tests/test_app/db/migrate/20130719024021_create_impressions_table.rb index d3dec32..39afc68 100644 --- a/tests/test_app/db/migrate/20130719024021_create_impressions_table.rb +++ b/tests/test_app/db/migrate/20130719024021_create_impressions_table.rb @@ -1,4 +1,4 @@ -class CreateImpressionsTable < ActiveRecord::Migration +class CreateImpressionsTable < ActiveRecord::Migration[4.2] def self.up create_table :impressions, :force => true do |t| t.string :impressionable_type diff --git a/tests/test_app/db/migrate/20150207135825_create_profiles.rb b/tests/test_app/db/migrate/20150207135825_create_profiles.rb index 008c8fb..4243898 100644 --- a/tests/test_app/db/migrate/20150207135825_create_profiles.rb +++ b/tests/test_app/db/migrate/20150207135825_create_profiles.rb @@ -1,4 +1,4 @@ -class CreateProfiles < ActiveRecord::Migration +class CreateProfiles < ActiveRecord::Migration[4.2] def change create_table :profiles do |t| t.string :username diff --git a/tests/test_app/db/migrate/20150207140310_create_friendly_id_slugs.rb b/tests/test_app/db/migrate/20150207140310_create_friendly_id_slugs.rb index bb80e48..15c1f92 100644 --- a/tests/test_app/db/migrate/20150207140310_create_friendly_id_slugs.rb +++ b/tests/test_app/db/migrate/20150207140310_create_friendly_id_slugs.rb @@ -1,4 +1,4 @@ -class CreateFriendlyIdSlugs < ActiveRecord::Migration +class CreateFriendlyIdSlugs < ActiveRecord::Migration[4.2] def self.up create_table :friendly_id_slugs do |t| diff --git a/tests/test_app/db/schema.rb b/tests/test_app/db/schema.rb index cac1d3b..3ca8356 100644 --- a/tests/test_app/db/schema.rb +++ b/tests/test_app/db/schema.rb @@ -1,80 +1,77 @@ -# encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. # -# It's strongly recommended to check this file into your version control system. +# It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(:version => 20150207140310) do +ActiveRecord::Schema.define(version: 2015_02_07_140310) do - create_table "articles", :force => true do |t| - t.string "name" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false - end - - create_table "friendly_id_slugs", :force => true do |t| - t.string "slug", :null => false - t.integer "sluggable_id", :null => false - t.string "sluggable_type", :limit => 40 + create_table "articles", force: :cascade do |t| + t.string "name" t.datetime "created_at" + t.datetime "updated_at" end - add_index "friendly_id_slugs", ["slug", "sluggable_type"], :name => "index_friendly_id_slugs_on_slug_and_sluggable_type", :unique => true - add_index "friendly_id_slugs", ["sluggable_id"], :name => "index_friendly_id_slugs_on_sluggable_id" - add_index "friendly_id_slugs", ["sluggable_type"], :name => "index_friendly_id_slugs_on_sluggable_type" - - create_table "impressions", :force => true do |t| - t.string "impressionable_type" - t.integer "impressionable_id" - t.integer "user_id" - t.string "controller_name" - t.string "action_name" - t.string "view_name" - t.string "request_hash" - t.string "ip_address" - t.string "session_hash" - t.text "message" - t.text "params" - t.text "referrer" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + create_table "friendly_id_slugs", force: :cascade do |t| + t.string "slug", null: false + t.integer "sluggable_id", null: false + t.string "sluggable_type", limit: 40 + t.datetime "created_at" + t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type", unique: true + t.index ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id" + t.index ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type" end - add_index "impressions", ["controller_name", "action_name", "ip_address"], :name => "controlleraction_ip_index" - add_index "impressions", ["controller_name", "action_name", "request_hash"], :name => "controlleraction_request_index" - add_index "impressions", ["controller_name", "action_name", "session_hash"], :name => "controlleraction_session_index" - add_index "impressions", ["impressionable_type", "impressionable_id", "ip_address"], :name => "poly_ip_index" - add_index "impressions", ["impressionable_type", "impressionable_id", "params"], :name => "poly_params_request_index" - add_index "impressions", ["impressionable_type", "impressionable_id", "request_hash"], :name => "poly_request_index" - add_index "impressions", ["impressionable_type", "impressionable_id", "session_hash"], :name => "poly_session_index" - add_index "impressions", ["impressionable_type", "message", "impressionable_id"], :name => "impressionable_type_message_index" - add_index "impressions", ["user_id"], :name => "index_impressions_on_user_id" - - create_table "posts", :force => true do |t| - t.string "name" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + create_table "impressions", force: :cascade do |t| + t.string "impressionable_type" + t.integer "impressionable_id" + t.integer "user_id" + t.string "controller_name" + t.string "action_name" + t.string "view_name" + t.string "request_hash" + t.string "ip_address" + t.string "session_hash" + t.text "message" + t.text "params" + t.text "referrer" + t.datetime "created_at" + t.datetime "updated_at" + t.index ["controller_name", "action_name", "ip_address"], name: "controlleraction_ip_index" + t.index ["controller_name", "action_name", "request_hash"], name: "controlleraction_request_index" + t.index ["controller_name", "action_name", "session_hash"], name: "controlleraction_session_index" + t.index ["impressionable_type", "impressionable_id", "ip_address"], name: "poly_ip_index" + t.index ["impressionable_type", "impressionable_id", "params"], name: "poly_params_request_index" + t.index ["impressionable_type", "impressionable_id", "request_hash"], name: "poly_request_index" + t.index ["impressionable_type", "impressionable_id", "session_hash"], name: "poly_session_index" + t.index ["impressionable_type", "message", "impressionable_id"], name: "impressionable_type_message_index" + t.index ["user_id"], name: "index_impressions_on_user_id" end - create_table "profiles", :force => true do |t| - t.string "username" - t.string "slug" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + create_table "posts", force: :cascade do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" end - create_table "widgets", :force => true do |t| - t.string "name" - t.integer "impressions_count", :default => 0 - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + create_table "profiles", force: :cascade do |t| + t.string "username" + t.string "slug" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "widgets", force: :cascade do |t| + t.string "name" + t.integer "impressions_count", default: 0 + t.datetime "created_at" + t.datetime "updated_at" end end diff --git a/tests/test_app/spec/controllers/articles_controller_spec.rb b/tests/test_app/spec/controllers/articles_controller_spec.rb index c41eda2..e519b3b 100644 --- a/tests/test_app/spec/controllers/articles_controller_spec.rb +++ b/tests/test_app/spec/controllers/articles_controller_spec.rb @@ -1,90 +1,113 @@ +# frozen_string_literal: false + require 'spec_helper' -describe ArticlesController do - fixtures :articles,:impressions,:posts,:widgets +describe ArticlesController, type: :controller do + fixtures :articles, :impressions, :posts, :widgets render_views - it "should make the impressionable_hash available" do - get "index" - response.body.include?("false").should eq true + it 'makes the impressionable_hash available' do + get :index + expect(response.body).to include('false') end - it "should log an impression with a message" do - get "index" - Impression.all.size.should eq 12 - Article.first.impressions.last.message.should eq "this is a test article impression" - Article.first.impressions.last.controller_name.should eq "articles" - Article.first.impressions.last.action_name.should eq "index" + it 'logs an impression with a message' do + get 'index' + + latest_impression = Article.first.impressions.last + + expect(Impression.all.size).to eq 12 + + expect(latest_impression.message).to eq 'this is a test article impression' + expect(latest_impression.controller_name).to eq 'articles' + expect(latest_impression.action_name).to eq 'index' end - it "should log an impression without a message" do - get "show", :id=> 1 - Impression.all.size.should eq 12 - Article.first.impressions.last.message.should eq nil - Article.first.impressions.last.controller_name.should eq "articles" - Article.first.impressions.last.action_name.should eq "show" + it 'logs an impression without a message' do + get :show, params: { id: 1 } + + latest_impression = Article.first.impressions.last + + expect(Impression.all.size).to eq 12 + + expect(latest_impression.message).to eq nil + expect(latest_impression.controller_name).to eq 'articles' + expect(latest_impression.action_name).to eq 'show' end - it "should log the user_id if user is authenticated (@current_user before_action method)" do + it 'logs the user_id if user is authenticated (@current_user before_action method)' do session[:user_id] = 123 - get "show", :id=> 1 - Article.first.impressions.last.user_id.should eq 123 + get :show, params: { id: 1 } + + expect(Article.first.impressions.last.user_id).to eq 123 end - it "should not log the user_id if user is authenticated" do - get "show", :id=> 1 - Article.first.impressions.last.user_id.should eq nil + it 'does not log the user_id if user is authenticated' do + get :show, params: { id: 1 } + + expect(Article.first.impressions.last.user_id).to eq nil end - it "should log the request_hash, ip_address, referrer and session_hash" do - get "show", :id=> 1 - Impression.last.request_hash.size.should eq 64 - Impression.last.ip_address.should eq "0.0.0.0" - Impression.last.session_hash.size.should eq 32 - Impression.last.referrer.should eq nil + it 'logs the request_hash, ip_address, referrer and session_hash' do + get :show, params: { id: 1 } + + impression = Impression.last + + expect(impression.request_hash.size).to eq 64 + expect(impression.ip_address).to eq '0.0.0.0' + expect(impression.session_hash.size).to eq 32 + expect(impression.referrer).to eq nil end # Capybara has change the way it works # We need to pass :type options in order to make include helper methods # see https://github.com/jnicklas/capybara#using-capybara-with-rspec - it "should log the referrer when you click a link", :type => :feature do + it 'logs the referrer when you click a link', type: :feature do + default_url_options[:host] = "test.host" + visit article_url(Article.first) - click_link "Same Page" - Impression.last.referrer.should eq "http://test.host/articles/1" + click_link 'Same Page' + expect(Impression.last.referrer).to eq 'http://test.host/articles/1' end - it "should log request with params (checked = true)" do - get "show", id: 1, checked: true - Impression.last.params.should eq({"checked"=>true}) - Impression.last.request_hash.size.should eq 64 - Impression.last.ip_address.should eq "0.0.0.0" - Impression.last.session_hash.size.should eq 32 - Impression.last.referrer.should eq nil + it 'logs request with params (checked = true)' do + get :show, params: { id: 1, checked: true } + + impression = Impression.last + + expect(impression.params).to eq({ 'checked' => "true" }) + expect(impression.request_hash.size).to eq 64 + expect(impression.ip_address).to eq '0.0.0.0' + expect(impression.session_hash.size).to eq 32 + expect(impression.referrer).to eq nil end - it "should log request with params {}" do - get "index" - Impression.last.params.should eq({}) - Impression.last.request_hash.size.should eq 64 - Impression.last.ip_address.should eq "0.0.0.0" - Impression.last.session_hash.size.should eq 32 - Impression.last.referrer.should eq nil + it 'logs request with params: {}' do + get 'index' + + impression = Impression.last + + expect(impression.params).to eq({}) + expect(impression.request_hash.size).to eq 64 + expect(impression.ip_address).to eq '0.0.0.0' + expect(impression.session_hash.size).to eq 32 + expect(impression.referrer).to eq nil end - describe "when filtering params" do + describe 'when filtering params' do before do @_filtered_params = Rails.application.config.filter_parameters Rails.application.config.filter_parameters = [:password] end - it "values should not be recorded" do - get "index", password: "best-password-ever" - Impression.last.params.should eq("password" => "[FILTERED]") - end - after do Rails.application.config.filter_parameters = @_filtered_params end + + it 'values should not be recorded' do + get 'index', params: { password: 'best-password-ever' } + expect(Impression.last.params).to eq('password' => '[FILTERED]') + end end end diff --git a/tests/test_app/spec/controllers/dummy_controller_spec.rb b/tests/test_app/spec/controllers/dummy_controller_spec.rb index 635b0f6..9ecd0ed 100644 --- a/tests/test_app/spec/controllers/dummy_controller_spec.rb +++ b/tests/test_app/spec/controllers/dummy_controller_spec.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: false + require 'spec_helper' -describe DummyController do +describe DummyController, type: :controller do fixtures :impressions render_views - it "should log impression at the per action level on non-restful controller" do - get "index" - Impression.all.size.should eq 12 + it 'logs impression at the per action level on non-restful controller' do + get 'index' + expect(Impression.all.size).to eq 12 end end diff --git a/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb b/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb index 8fed026..2b1d9e0 100644 --- a/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb +++ b/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb @@ -2,158 +2,192 @@ require 'spec_helper' # we use the posts controller as it uses the impressionsist module. any such controller would do. describe DummyController do - before do @impression_count = Impression.all.size end describe "impressionist filter uniqueness" do - - it "should ignore uniqueness if not requested" do + it "ignore uniqueness if not requested" do controller.impressionist_subapp_filter controller.impressionist_subapp_filter - Impression.should have(@impression_count + 2).records + + expect(Impression.count).to eq(@impression_count + 2) end - it "should recognize unique session" do - controller.stub(:session_hash).and_return(request.session_options[:id]) + it "recognize unique session" do + allow(controller).to receive(:session_hash).and_return(request.session_options[:id]) + controller.impressionist_subapp_filter(unique: [:session_hash]) controller.impressionist_subapp_filter(unique: [:session_hash]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to eq(@impression_count + 1) end - it "should recognize unique ip" do - controller.request.stub(:remote_ip).and_return("1.2.3.4") + it "recognize unique ip" do + allow(controller).to receive(:remote_ip).and_return("1.2.3.4") + controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:ip_address]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize unique request" do + it "recognize unique request" do controller.impressionist_subapp_filter(unique: [:request_hash]) controller.impressionist_subapp_filter(unique: [:request_hash]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize unique action" do - controller.stub(:action_name).and_return("test_action") + it "recognize unique action" do + allow(controller).to receive(:action_name).and_return("test_action") + controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:action_name]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize unique controller" do - controller.stub(:controller_name).and_return("post") + it "recognize unique controller" do + allow(controller).to receive(:controller_name).and_return("post") + controller.impressionist_subapp_filter(unique: [:controller_name]) controller.impressionist_subapp_filter(unique: [:controller_name]) - Impression.should have(@impression_count + 1).records + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize unique user" do - controller.stub(:user_id).and_return(42) + it "recognize unique user" do + allow(controller).to receive(:user_id).and_return(42) + controller.impressionist_subapp_filter(unique: [:user_id]) controller.impressionist_subapp_filter(unique: [:user_id]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize unique referer" do - controller.request.stub(:referer).and_return("http://foo/bar") + it "recognize unique referer" do + allow(controller.request).to receive(:referer).and_return("http://foo/bar") + controller.impressionist_subapp_filter(unique: [:referrer]) controller.impressionist_subapp_filter(unique: [:referrer]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize unique id" do - controller.stub(:params).and_return({:id => "666"}) # for correct impressionable id in filter + it "recognize unique id" do + allow(controller).to receive(:params).and_return({ :id => "666" }) # for correct impressionable id in filter + controller.impressionist_subapp_filter(unique: [:impressionable_id]) controller.impressionist_subapp_filter(unique: [:impressionable_id]) - Impression.should have(@impression_count + 1).records + expect(Impression.count).to equal(@impression_count + 1) end # extra redundant test for important controller and action combination. - it "should recognize different controller and action" do - controller.stub(:controller_name).and_return("post") - controller.stub(:action_name).and_return("test_action") + it "recognize different controller and action" do + allow(controller).to receive(:controller_name).and_return("post") + allow(controller).to receive(:action_name).and_return("test_action") + controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) - Impression.should have(@impression_count + 1).records - controller.stub(:action_name).and_return("another_action") + + expect(Impression.count).to equal(@impression_count + 1) + + allow(controller).to receive(:action_name).and_return("another_action") + controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) - Impression.should have(@impression_count + 2).records - controller.stub(:controller_name).and_return("article") + + expect(Impression.count).to equal(@impression_count + 2) + + allow(controller).to receive(:controller_name).and_return("article") + controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) - Impression.should have(@impression_count + 3).records + + expect(Impression.count).to equal(@impression_count + 3) end - it "should recognize different action" do - controller.stub(:action_name).and_return("test_action") + it "recognize different action" do + allow(controller).to receive(:action_name).and_return("test_action") controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:action_name]) - Impression.should have(@impression_count + 1).records - controller.stub(:action_name).and_return("another_action") + expect(Impression.count).to equal(@impression_count + 1) + allow(controller).to receive(:action_name).and_return("another_action") controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:action_name]) - Impression.should have(@impression_count + 2).records + expect(Impression.count).to equal(@impression_count + 2) end - it "should recognize different controller" do - controller.stub(:controller_name).and_return("post") + it "recognize different controller" do + allow(controller).to receive(:controller_name).and_return("post") controller.impressionist_subapp_filter(unique: [:controller_name]) controller.impressionist_subapp_filter(unique: [:controller_name]) - Impression.should have(@impression_count + 1).records - controller.stub(:controller_name).and_return("article") + expect(Impression.count).to equal(@impression_count + 1) + allow(controller).to receive(:controller_name).and_return("article") controller.impressionist_subapp_filter(unique: [:controller_name]) controller.impressionist_subapp_filter(unique: [:controller_name]) - Impression.should have(@impression_count + 2).records + expect(Impression.count).to equal(@impression_count + 2) end - it "should recognize different session" do - controller.stub(:session_hash).and_return("foo") + it "recognize different session" do + allow(controller).to receive(:session_hash).and_return("foo") controller.impressionist_subapp_filter(unique: [:session_hash]) controller.impressionist_subapp_filter(unique: [:session_hash]) - Impression.should have(@impression_count + 1).records - controller.stub(:session_hash).and_return("bar") + expect(Impression.count).to equal(@impression_count + 1) + allow(controller).to receive(:session_hash).and_return("bar") controller.impressionist_subapp_filter(unique: [:session_hash]) controller.impressionist_subapp_filter(unique: [:session_hash]) - Impression.should have(@impression_count + 2).records + expect(Impression.count).to equal(@impression_count + 2) end - it "should recognize different ip" do - controller.request.stub(:remote_ip).and_return("1.2.3.4") + it "recognize different ip" do + allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4") + controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:ip_address]) - Impression.should have(@impression_count + 1).records - controller.request.stub(:remote_ip).and_return("5.6.7.8") + + expect(Impression.count).to equal(@impression_count + 1) + + allow(controller.request).to receive(:remote_ip).and_return("5.6.7.8") + controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:ip_address]) - Impression.should have(@impression_count + 2).records + + expect(Impression.count).to equal(@impression_count + 2) end - it "should recognize different referer" do - controller.request.stub(:referer).and_return("http://foo/bar") + it "recognize different referer" do + allow(controller.request).to receive(:referer).and_return("http://foo/bar") + controller.impressionist_subapp_filter(unique: [:referrer]) controller.impressionist_subapp_filter(unique: [:referrer]) - Impression.should have(@impression_count + 1).records - controller.request.stub(:referer).and_return("http://bar/fo") + + expect(Impression.count).to equal(@impression_count + 1) + + allow(controller.request).to receive(:referer).and_return("http://bar/fo") + controller.impressionist_subapp_filter(unique: [:referrer]) controller.impressionist_subapp_filter(unique: [:referrer]) - Impression.should have(@impression_count + 2).records + expect(Impression.count).to equal(@impression_count + 2) end - it "should recognize different id" do - controller.stub(:params).and_return({:id => "666"}) # for correct impressionable id in filter + it "recognize different id" do + allow(controller).to receive(:params).and_return({ :id => "666" }) # for correct impressionable id in filter + controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id]) controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id]) - controller.stub(:params).and_return({:id => "42"}) # for correct impressionable id in filter + + allow(controller).to receive(:params).and_return({ :id => "42" }) # for correct impressionable id in filter + controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id]) controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id]) - Impression.should have(@impression_count + 2).records + + expect(Impression.count).to equal(@impression_count + 2) end - it "should recognize combined uniqueness" do - controller.stub(:action_name).and_return("test_action") + it "recognize combined uniqueness" do + allow(controller).to receive(:action_name).and_return("test_action") + controller.impressionist_subapp_filter(unique: [:ip_address, :request_hash, :action_name]) controller.impressionist_subapp_filter(unique: [:request_hash, :ip_address, :action_name]) controller.impressionist_subapp_filter(unique: [:request_hash, :action_name]) @@ -162,110 +196,142 @@ describe DummyController do controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:request_hash]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize combined non-uniqueness" do - controller.stub(:action_name).and_return(nil) - controller.impressionist_subapp_filter(unique: [:ip_address, :action_name]) - controller.stub(:action_name).and_return("test_action") - controller.impressionist_subapp_filter(unique: [:ip_address, :action_name]) - controller.stub(:action_name).and_return("another_action") - controller.impressionist_subapp_filter(unique: [:ip_address, :action_name]) - Impression.should have(@impression_count + 3).records - end + it "recognize combined non-uniqueness" do + allow(controller).to receive(:action_name).and_return(nil) + controller.impressionist_subapp_filter(unique: [:ip_address, :action_name]) + + allow(controller).to receive(:action_name).and_return("test_action") + + controller.impressionist_subapp_filter(unique: [:ip_address, :action_name]) + + allow(controller).to receive(:action_name).and_return("another_action") + + controller.impressionist_subapp_filter(unique: [:ip_address, :action_name]) + + expect(Impression.count).to equal(@impression_count + 3) + end end describe "impressionist method uniqueness for impressionables" do - # in this test we reuse the post model. might break if model changes. - it "should ignore uniqueness if not requested" do + it "ignore uniqueness if not requested" do impressionable = Post.create controller.impressionist impressionable controller.impressionist impressionable - Impression.should have(@impression_count + 2).records + expect(Impression.count).to equal(@impression_count + 2) end - it "should recognize unique session" do - controller.stub(:session_hash).and_return(request.session_options[:id]) + it "recognize unique session" do + allow(controller).to receive(:session_hash).and_return(request.session_options[:id]) + impressionable = Post.create controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:session_hash]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize unique ip" do - controller.request.stub(:remote_ip).and_return("1.2.3.4") + it "recognize unique ip" do + allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4") + impressionable = Post.create controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:ip_address]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize unique request" do + it "recognize unique request" do impressionable = Post.create controller.impressionist(impressionable, nil, :unique => [:request_hash]) controller.impressionist(impressionable, nil, :unique => [:request_hash]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize unique user" do - controller.stub(:user_id).and_return(666) + it "recognize unique user" do + allow(controller).to receive(:user_id).and_return(666) + impressionable = Post.create controller.impressionist(impressionable, nil, :unique => [:user_id]) controller.impressionist(impressionable, nil, :unique => [:user_id]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize unique referer" do - controller.request.stub(:referer).and_return("http://foo/bar") + it "recognize unique referer" do + allow(controller.request).to receive(:referer).and_return("http://foo/bar") + impressionable = Post.create controller.impressionist(impressionable, nil, :unique => [:referrer]) controller.impressionist(impressionable, nil, :unique => [:referrer]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize different session" do + it "recognize different session" do impressionable = Post.create - controller.stub(:session_hash).and_return("foo") + + allow(controller).to receive(:session_hash).and_return("foo") + controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:session_hash]) - Impression.should have(@impression_count + 1).records - controller.stub(:session_hash).and_return("bar") + + expect(Impression.count).to equal(@impression_count + 1) + + allow(controller).to receive(:session_hash).and_return("bar") + controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:session_hash]) - Impression.should have(@impression_count + 2).records + + expect(Impression.count).to equal(@impression_count + 2) end - it "should recognize different ip" do - controller.request.stub(:remote_ip).and_return("1.2.3.4") + it "recognize different ip" do + allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4") + impressionable = Post.create controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:ip_address]) - Impression.should have(@impression_count + 1).records - controller.request.stub(:remote_ip).and_return("5.6.7.8") + + expect(Impression.count).to equal(@impression_count + 1) + + allow(controller.request).to receive(:remote_ip).and_return("5.6.7.8") + controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:ip_address]) - Impression.should have(@impression_count + 2).records + + expect(Impression.count).to equal(@impression_count + 2) end - it "should recognize different user" do + it "recognize different user" do impressionable = Post.create - controller.stub(:user_id).and_return(666) + + allow(controller).to receive(:user_id).and_return(666) + controller.impressionist(impressionable, nil, :unique => [:user_id]) controller.impressionist(impressionable, nil, :unique => [:user_id]) - Impression.should have(@impression_count + 1).records - controller.stub(:user_id).and_return(42) + + expect(Impression.count).to equal(@impression_count + 1) + + allow(controller).to receive(:user_id).and_return(42) + controller.impressionist(impressionable, nil, :unique => [:user_id]) controller.impressionist(impressionable, nil, :unique => [:user_id]) - Impression.should have(@impression_count + 2).records + + expect(Impression.count).to equal(@impression_count + 2) end - it "should recognize combined uniqueness" do + it "recognize combined uniqueness" do impressionable = Post.create - controller.stub(:session_hash).and_return("foo") + allow(controller).to receive(:session_hash).and_return("foo") + controller.impressionist(impressionable, nil, :unique => [:ip_address, :request_hash, :session_hash]) controller.impressionist(impressionable, nil, :unique => [:request_hash, :ip_address, :session_hash]) controller.impressionist(impressionable, nil, :unique => [:request_hash, :session_hash]) @@ -274,64 +340,62 @@ describe DummyController do controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:request_hash]) - Impression.should have(@impression_count + 1).records + + expect(Impression.count).to equal(@impression_count + 1) end - it "should recognize combined non-uniqueness" do + it "recognize combined non-uniqueness" do impressionable = Post.create - controller.stub(:session_hash).and_return(nil) + allow(controller).to receive(:session_hash).and_return(nil) controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash]) - controller.stub(:session_hash).and_return("foo") + allow(controller).to receive(:session_hash).and_return("foo") controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash]) - controller.stub(:session_hash).and_return("bar") + allow(controller).to receive(:session_hash).and_return("bar") controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash]) - Impression.should have(@impression_count + 3).records + expect(Impression.count).to equal(@impression_count + 3) end - end describe "impressionist filter and method uniqueness" do - - it "should recognize uniqueness" do + it "recognize uniqueness" do impressionable = Post.create - controller.stub(:controller_name).and_return("posts") # for correct impressionable type in filter - controller.stub(:params).and_return({:id => impressionable.id.to_s}) # for correct impressionable id in filter - controller.stub(:session_hash).and_return("foo") - controller.request.stub(:remote_ip).and_return("1.2.3.4") + allow(controller).to receive(:controller_name).and_return("posts") # for correct impressionable type in filter + allow(controller).to receive(:params).and_return({ :id => impressionable.id.to_s }) # for correct impressionable id in filter + allow(controller).to receive(:session_hash).and_return("foo") + allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4") # order of the following methods is important for the test! controller.impressionist_subapp_filter(unique: [:ip_address, :request_hash, :session_hash]) controller.impressionist(impressionable, nil, :unique => [:ip_address, :request_hash, :session_hash]) - Impression.should have(@impression_count + 1).records + expect(Impression.count).to equal(@impression_count + 1) end - end describe 'impressionist with friendly id' do - it 'should unique' do - impressionable = Profile.create({username: 'test_profile', slug: 'test_profile'}) + it 'unique' do + impressionable = Profile.create({ username: 'test_profile', slug: 'test_profile' }) - controller.stub(:controller_name).and_return('profile') - controller.stub(:action_name).and_return('show') - controller.stub(:params).and_return({id: impressionable.slug}) - controller.request.stub(:remote_ip).and_return('1.2.3.4') + allow(controller).to receive(:controller_name).and_return('profile') + allow(controller).to receive(:action_name).and_return('show') + allow(controller).to receive(:params).and_return({ id: impressionable.slug }) + allow(controller.request).to receive(:remote_ip).and_return('1.2.3.4') controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id]) controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id]) - Impression.should have(@impression_count + 1).records + expect(Impression.count).to equal(@impression_count + 1) end end shared_examples_for 'an impressionable action' do - it 'should record an impression' do + it 'record an impression' do controller.impressionist_subapp_filter(condition) - Impression.should have(@impression_count + 1).records + expect(Impression.count).to equal(@impression_count + 1) end end shared_examples_for 'an unimpressionable action' do - it 'should record an impression' do + it 'record an impression' do controller.impressionist_subapp_filter(condition) - Impression.should have(@impression_count).records + expect(Impression.count).to equal(@impression_count) end end @@ -339,27 +403,29 @@ describe DummyController do describe ":if condition" do context "true condition" do before do - controller.stub(:true_condition).and_return(true) - end - it_behaves_like 'an impressionable action' do - let(:condition) {{ if: :true_condition }} + allow(controller).to receive(:true_condition).and_return(true) end it_behaves_like 'an impressionable action' do - let(:condition) {{ if: lambda { true } }} + let(:condition) { { if: :true_condition } } + end + + it_behaves_like 'an impressionable action' do + let(:condition) { { if: -> { true } } } end end context "false condition" do before do - controller.stub(:false_condition).and_return(false) - end - it_behaves_like 'an unimpressionable action' do - let(:condition) {{ if: :false_condition }} + allow(controller).to receive(:false_condition).and_return(false) end it_behaves_like 'an unimpressionable action' do - let(:condition) {{ if: lambda { false } }} + let(:condition) { { if: :false_condition } } + end + + it_behaves_like 'an unimpressionable action' do + let(:condition) { { if: -> { false } } } end end end @@ -367,30 +433,31 @@ describe DummyController do describe ":unless condition" do context "true condition" do before do - controller.stub(:true_condition).and_return(true) - end - it_behaves_like 'an unimpressionable action' do - let(:condition) {{ unless: :true_condition }} + allow(controller).to receive(:true_condition).and_return(true) end it_behaves_like 'an unimpressionable action' do - let(:condition) {{ unless: lambda { true } }} + let(:condition) { { unless: :true_condition } } + end + + it_behaves_like 'an unimpressionable action' do + let(:condition) { { unless: -> { true } } } end end context "false condition" do before do - controller.stub(:false_condition).and_return(false) - end - it_behaves_like 'an impressionable action' do - let(:condition) {{ unless: :false_condition }} + allow(controller).to receive(:false_condition).and_return(false) end it_behaves_like 'an impressionable action' do - let(:condition) {{ unless: lambda { false } }} + let(:condition) { { unless: :false_condition } } + end + + it_behaves_like 'an impressionable action' do + let(:condition) { { unless: -> { false } } } end end end end end - diff --git a/tests/test_app/spec/controllers/posts_controller_spec.rb b/tests/test_app/spec/controllers/posts_controller_spec.rb index 78827e2..cd54863 100644 --- a/tests/test_app/spec/controllers/posts_controller_spec.rb +++ b/tests/test_app/spec/controllers/posts_controller_spec.rb @@ -1,28 +1,36 @@ require 'spec_helper' describe PostsController do - it "should log impression at the action level" do - get "show", :id=> 1 - Impression.all.size.should eq 12 - Impression.last.controller_name.should eq "posts" - Impression.last.action_name.should eq "show" - Impression.last.impressionable_type.should eq "Post" - Impression.last.impressionable_id.should eq 1 + it "logs impression at the action level" do + get :show, params: { id: 1 } + + expect(Impression.all.size).to eq 12 + + impression = Impression.last + + expect(impression.controller_name).to eq "posts" + expect(impression.action_name).to eq "show" + expect(impression.impressionable_type).to eq "Post" + expect(impression.impressionable_id).to eq 1 end - it "should log the user_id if user is authenticated (current_user helper method)" do + it "logs the user_id if user is authenticated (current_user helper method)" do session[:user_id] = 123 - get "show", :id=> 1 - Post.first.impressions.last.user_id.should eq 123 + get :show, params: { id: 1 } + expect(Post.first.impressions.last.user_id).to eq 123 end - it "should log impression at the action level with params" do - get "show", id: 1, checked: true - Impression.all.size.should eq 12 - Impression.last.params.should eq({"checked"=>true}) - Impression.last.controller_name.should eq "posts" - Impression.last.action_name.should eq "show" - Impression.last.impressionable_type.should eq "Post" - Impression.last.impressionable_id.should eq 1 + it "logs impression at the action level with params" do + get :show, params: { id: 1, checked: true } + + expect(Impression.all.size).to eq 12 + + impression = Impression.last + + expect(impression.params).to eq({ "checked" => "true" }) + expect(impression.controller_name).to eq "posts" + expect(impression.action_name).to eq "show" + expect(impression.impressionable_type).to eq "Post" + expect(impression.impressionable_id).to eq 1 end end diff --git a/tests/test_app/spec/controllers/widgets_controller_spec.rb b/tests/test_app/spec/controllers/widgets_controller_spec.rb index 61f7c28..35edd93 100644 --- a/tests/test_app/spec/controllers/widgets_controller_spec.rb +++ b/tests/test_app/spec/controllers/widgets_controller_spec.rb @@ -1,91 +1,103 @@ require 'spec_helper' describe WidgetsController do - - before(:each) do + before do @widget = Widget.find(1) - Widget.stub(:find).and_return(@widget) + allow(Widget).to receive(:find).and_return(@widget) end - it "should log impression at the per action level" do - get "show", :id=> 1 - Impression.all.size.should eq 12 - get "index" - Impression.all.size.should eq 13 - get "new" - Impression.all.size.should eq 13 + it "logs impression at the per action level" do + get :show, params: { id: 1 } + expect(Impression.all.size).to eq 12 + + get :index + + expect(Impression.all.size).to eq 13 + + get :new + + expect(Impression.all.size).to eq 13 end - it "should not log impression when user-agent is in wildcard list" do - request.stub(:user_agent).and_return('somebot') - get "show", :id=> 1 - Impression.all.size.should eq 11 + it "does not log impression when user-agent is in wildcard list" do + request.env['HTTP_USER_AGENT'] = 'somebot' + + get :show, params: { id: 1 } + expect(Impression.all.size).to eq 11 end - it "should not log impression when user-agent is in the bot list" do - request.stub(:user_agent).and_return('Acoon Robot v1.50.001') - get "show", :id=> 1 - Impression.all.size.should eq 11 + it "does not log impression when user-agent is in the bot list" do + request.env['HTTP_USER_AGENT'] = 'Acoon Robot v1.50.001' + + get :show, params: { id: 1 } + expect(Impression.all.size).to eq 11 end context "impressionist unique options" do - - it "should log unique impressions at the per action level" do - get "show", :id=> 1 - Impression.all.size.should eq 12 - get "show", :id=> 2 - Impression.all.size.should eq 13 - get "show", :id => 2 - Impression.all.size.should eq 13 - get "index" - Impression.all.size.should eq 14 + it "logs unique impressions at the per action level" do + get :show, params: { id: 1 } + expect(Impression.all.size).to eq 12 + get :show, params: { id: 2 } + expect(Impression.all.size).to eq 13 + get :show, params: { id: 2 } + expect(Impression.all.size).to eq 13 + get :index + expect(Impression.all.size).to eq 14 end - it "should log unique impressions only once per id" do - get "show", :id=> 1 - Impression.all.size.should eq 12 - get "show", :id=> 2 - Impression.all.size.should eq 13 + it "logs unique impressions only once per id" do + get :show, params: { id: 1 } + expect(Impression.all.size).to eq 12 + get :show, params: { id: 2 } + expect(Impression.all.size).to eq 13 - get "show", :id => 2 - Impression.all.size.should eq 13 + get :show, params: { id: 2 } + expect(Impression.all.size).to eq 13 - get "index" - Impression.all.size.should eq 14 + get :index + expect(Impression.all.size).to eq 14 end - end context "Impresionist unique params options" do - it "should log unique impressions at the per action and params level" do - get "show", :id => 1 - Impression.all.size.should eq 12 - get "show", :id => 2, checked: true - Impression.all.size.should eq 13 - get "show", :id => 2, checked: false - Impression.all.size.should eq 14 - get "index" - Impression.all.size.should eq 15 + it "logs unique impressions at the per action and params level" do + get :show, params: { id: 1 } + expect(Impression.all.size).to eq 12 + + get :show, params: { id: 2, checked: true } + expect(Impression.all.size).to eq 13 + + get :show, params: { id: 2, checked: false } + expect(Impression.all.size).to eq 14 + + get :index + expect(Impression.all.size).to eq 15 end - it "should not log impression for same params and same id" do - get "show", :id => 1 - Impression.all.size.should eq 12 - get "show", :id => 1 - Impression.all.size.should eq 12 - get "show", :id => 1, checked: true - Impression.all.size.should eq 13 - get "show", :id => 1, checked: false - Impression.all.size.should eq 14 - get "show", :id => 1, checked: true - Impression.all.size.should eq 14 - get "show", :id => 1, checked: false - Impression.all.size.should eq 14 - get "show", :id => 1 - Impression.all.size.should eq 14 - get "show", :id => 2 - Impression.all.size.should eq 15 + it "does not log impression for same params and same id" do + get :show, params: { id: 1 } + expect(Impression.all.size).to eq 12 + + get :show, params: { id: 1 } + expect(Impression.all.size).to eq 12 + + get :show, params: { id: 1, checked: true } + expect(Impression.all.size).to eq 13 + + get :show, params: { id: 1, checked: false } + expect(Impression.all.size).to eq 14 + + get :show, params: { id: 1, checked: true } + expect(Impression.all.size).to eq 14 + + get :show, params: { id: 1, checked: false } + expect(Impression.all.size).to eq 14 + + get :show, params: { id: 1 } + expect(Impression.all.size).to eq 14 + + get :show, params: { id: 2 } + expect(Impression.all.size).to eq 15 end end - end diff --git a/tests/test_app/spec/initializers/initializers_spec.rb b/tests/test_app/spec/initializers/initializers_spec.rb index 44d06ca..eb9ba5d 100644 --- a/tests/test_app/spec/initializers/initializers_spec.rb +++ b/tests/test_app/spec/initializers/initializers_spec.rb @@ -3,18 +3,19 @@ require 'spec_helper' describe Impressionist do let(:imp) { RUBY_VERSION.match("1.8") ? "is_impressionable" : :is_impressionable } - it "should be extended from ActiveRecord::Base" do + it "is extended from ActiveRecord::Base" do expect(ActiveRecord::Base).to respond_to(imp) - #ActiveRecord::Base.methods.include?(method).should be_true + # ActiveRecord::Base.methods.include?(method).should be_truthy end - it "should include methods in ApplicationController" do + it "includes methods in ApplicationController" do method = RUBY_VERSION.match("1.8") ? "impressionist" : :impressionist expect(ApplicationController).to respond_to(method) end - it "should include the before_action method in ApplicationController" do - filters = ApplicationController._process_action_callbacks.select { |c| c.kind == :before } - filters.collect{|filter|filter.filter}.include?(:impressionist_app_filter).should be_true + it "includes the before_action method in ApplicationController" do + filters = ApplicationController._process_action_callbacks.select { |c| c.kind == :before } + + expect(filters.collect(&:filter)).to include(:impressionist_app_filter) end end diff --git a/tests/test_app/spec/models/bots_spec.rb b/tests/test_app/spec/models/bots_spec.rb index a298358..ba10ac7 100644 --- a/tests/test_app/spec/models/bots_spec.rb +++ b/tests/test_app/spec/models/bots_spec.rb @@ -1,27 +1,26 @@ require 'spec_helper' describe Impressionist::Bots do - describe "self.bot?" do it "is true if user_agent is matches wild card" do - Impressionist::Bots.bot?("google.com bot").should be_true + expect(described_class).to be_bot("google.com bot") end it "is true if user_agent is on bot list" do - Impressionist::Bots.bot?("A-Online Search").should be_true + expect(described_class).to be_bot("A-Online Search") end it "is false if user_agent is blank" do - Impressionist::Bots.bot?("").should be_false - Impressionist::Bots.bot?(nil).should be_false + expect(described_class).not_to be_bot("") + expect(described_class).not_to be_bot(nil) end it "is false if user_agent is safe" do - Impressionist::Bots.bot?('127.0.0.1').should be_false + expect(described_class).not_to be_bot('127.0.0.1') end it "is false if user_agent not given" do - Impressionist::Bots.bot?.should be_false + expect(described_class).not_to be_bot end end -end \ No newline at end of file +end diff --git a/tests/test_app/spec/models/counter_caching_spec.rb b/tests/test_app/spec/models/counter_caching_spec.rb index 936f347..10852b6 100644 --- a/tests/test_app/spec/models/counter_caching_spec.rb +++ b/tests/test_app/spec/models/counter_caching_spec.rb @@ -5,47 +5,45 @@ describe Impression do let(:widget) { Widget.find(1) } - before(:each) do - Impression.destroy_all + before do + described_class.destroy_all end describe "self#impressionist_counter_caching?" do - it "should know when counter caching is enabled" do - Widget.should be_impressionist_counter_caching + it "knows when counter caching is enabled" do + expect(Widget).to be_impressionist_counter_caching end - it "should know when counter caching is disabled" do - Article.should_not be_impressionist_counter_caching + it "knows when counter caching is disabled" do + expect(Article).not_to be_impressionist_counter_caching end end describe "self#counter_caching?" do - it "should know when counter caching is enabled" do - ActiveSupport::Deprecation.should_receive(:warn) - Widget.should be_counter_caching + it "knows when counter caching is enabled" do + allow(ActiveSupport::Deprecation).to receive(:warn) + expect(Widget).to be_counter_caching end - it "should know when counter caching is disabled" do - ActiveSupport::Deprecation.should_receive(:warn) - Article.should_not be_counter_caching + it "knows when counter caching is disabled" do + allow(ActiveSupport::Deprecation).to receive(:warn) + expect(Article).not_to be_counter_caching end - end describe "#update_impressionist_counter_cache" do - it "should update the counter cache column to reflect the correct number of impressions" do - expect { - widget.impressions.create(:request_hash => 'abcd1234') - widget.reload - }.to change(widget, :impressions_count).from(0).to(1) + it "updates the counter cache column to reflect the correct number of impressions" do + expect do + widget.impressions.create(:request_hash => 'abcd1234') + widget.reload + end.to change(widget, :impressions_count).from(0).to(1) end - it "should not update the timestamp on the impressable" do - expect { - widget.impressions.create(:request_hash => 'abcd1234') - widget.reload - }.to_not change(widget, :updated_at) + it "does not update the timestamp on the impressable" do + expect do + widget.impressions.create(:request_hash => 'abcd1234') + widget.reload + end.not_to change(widget, :updated_at) end end - end diff --git a/tests/test_app/spec/models/model_spec.rb b/tests/test_app/spec/models/model_spec.rb index f666edd..c3e0505 100644 --- a/tests/test_app/spec/models/model_spec.rb +++ b/tests/test_app/spec/models/model_spec.rb @@ -1,70 +1,70 @@ require 'spec_helper' describe Impression do - fixtures :articles,:impressions,:posts,:profiles + fixtures :articles, :impressions, :posts, :profiles - before(:each) do + before do @article = Article.find(1) end - it "should save a blank impression for an Article that has 10 impressions" do + it "saves a blank impression for an Article that has 10 impressions" do @article.impressions.create - @article.impressions.size.should eq 12 + expect(@article.impressions.size).to eq 12 end - it "should save an impression with a message" do - @article.impressions.create(:message=>"test message") - @article.impressions.last.message.should eq "test message" + it "saves an impression with a message" do + @article.impressions.create(:message => "test message") + expect(@article.impressions.last.message).to eq "test message" end - it "should return the impression count for the message specified" do + it "returns the impression count for the message specified" do @article.impressions.create(:message => "pageview") @article.impressions.create(:message => "pageview") @article.impressions.create(:message => "visit") - @article.impressionist_count(:message => "pageview", :filter => :all).should eq 2 + expect(@article.impressionist_count(:message => "pageview", :filter => :all)).to eq 2 end - it "should return the impression count for all with no date range specified" do - @article.impressionist_count(:filter=>:all).should eq 11 + it "returns the impression count for all with no date range specified" do + expect(@article.impressionist_count(:filter => :all)).to eq 11 end - it "should return unique impression count with no date range specified" do - @article.impressionist_count.should eq 9 + it "returns unique impression count with no date range specified" do + expect(@article.impressionist_count).to eq 9 end - it "should return impression count with only start date specified" do - @article.impressionist_count(:start_date=>"2011-01-01",:filter=>:all).should eq 8 + it "returns impression count with only start date specified" do + expect(@article.impressionist_count(:start_date => "2011-01-01", :filter => :all)).to eq 8 end - it "should return impression count with whole date range specified" do - @article.impressionist_count(:start_date=>"2011-01-01",:end_date=>"2011-01-02",:filter=>:all).should eq 7 + it "returns impression count with whole date range specified" do + expect(@article.impressionist_count(:start_date => "2011-01-01", :end_date => "2011-01-02", :filter => :all)).to eq 7 end - it "should return unique impression count with only start date specified" do - @article.impressionist_count(:start_date=>"2011-01-01").should eq 7 + it "returns unique impression count with only start date specified" do + expect(@article.impressionist_count(:start_date => "2011-01-01")).to eq 7 end - it "should return unique impression count with date range specified" do - @article.impressionist_count(:start_date=>"2011-01-01",:end_date=>"2011-01-02").should eq 7 + it "returns unique impression count with date range specified" do + expect(@article.impressionist_count(:start_date => "2011-01-01", :end_date => "2011-01-02")).to eq 7 end - it "should return unique impression count using ip address (which in turn eliminates duplicate request_hashes)" do - @article.impressionist_count(:filter=>:ip_address).should eq 8 + it "returns unique impression count using ip address (which in turn eliminates duplicate request_hashes)" do + expect(@article.impressionist_count(:filter => :ip_address)).to eq 8 end - it "should return unique impression count using session_hash (which in turn eliminates duplicate request_hashes)" do - @article.impressionist_count(:filter=>:session_hash).should eq 7 + it "returns unique impression count using session_hash (which in turn eliminates duplicate request_hashes)" do + expect(@article.impressionist_count(:filter => :session_hash)).to eq 7 end # tests :dependent => :delete_all - it "should delete impressions on deletion of impressionable" do - #impressions_count = Impression.all.size + it "deletes impressions on deletion of impressionable" do + # impressions_count = Impression.all.size a = Article.create i = a.impressions.create a.destroy - a.destroyed?.should be_true - Impression.exists?(i.id).should be_false - end + expect(a).to be_destroyed + expect(described_class).not_to exist(i.id) + end end diff --git a/tests/test_app/spec/rails_generators/rails_generators_spec.rb b/tests/test_app/spec/rails_generators/rails_generators_spec.rb index 076abdf..91f7637 100644 --- a/tests/test_app/spec/rails_generators/rails_generators_spec.rb +++ b/tests/test_app/spec/rails_generators/rails_generators_spec.rb @@ -1,23 +1,23 @@ require 'spec_helper' require 'systemu' -# FIXME this test might break the others if run before them +# FIXME: this test might break the others if run before them # started fixing @nbit001 describe Impressionist, :migration do - fixtures :articles,:impressions,:posts,:profiles - it "should delete existing migration and generate the migration file" do + fixtures :articles, :impressions, :posts, :profiles + it "deletes existing migration and generate the migration file" do pending migrations_dir = "#{Rails.root}/db/migrate" impressions_migration = Dir.entries(migrations_dir).grep(/impressions/)[0] - File.delete("#{migrations_dir}/#{impressions_migration}") unless impressions_migration.blank? + File.delete("#{migrations_dir}/#{impressions_migration}") if impressions_migration.present? generator_output = systemu("rails g impressionist")[1] migration_name = generator_output.split("migrate/")[1].strip - Dir.entries(migrations_dir).include?(migration_name).should be_true + Dir.entries(migrations_dir).include?(migration_name).should be_truthy end - it "should run the migration created in the previous spec" do + it "runs the migration created in the previous spec" do pending migrate_output = systemu("rake db:migrate RAILS_ENV=test") - migrate_output[1].include?("CreateImpressionsTable: migrated").should be_true + migrate_output[1].include?("CreateImpressionsTable: migrated").should be_truthy end end diff --git a/tests/test_app/spec/spec_helper.rb b/tests/test_app/spec/spec_helper.rb index 111cd05..0d60fb1 100644 --- a/tests/test_app/spec/spec_helper.rb +++ b/tests/test_app/spec/spec_helper.rb @@ -5,22 +5,18 @@ unless ENV['CI'] SimpleCov.start 'rails' end -require File. -expand_path("../../config/environment", __FILE__) +require File.expand_path('../config/environment', __dir__) require 'rspec/rails' require 'capybara/rails' # Custom matchers and macros, etc... -Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| +Dir[Rails.root.join("spec/support/**/*.rb")].sort.each do |f| require f -} +end RSpec.configure do |config| - - # in order to pass tags(symbols) as true values - # you need to tell rspec to do so by - config.treat_symbols_as_metadata_keys_with_true_values = true + config.infer_spec_type_from_file_location! config.mock_with :rspec @@ -39,5 +35,4 @@ RSpec.configure do |config| def logger Rails.logger end - end diff --git a/upgrade_migrations/version_0_3_0.rb b/upgrade_migrations/version_0_3_0.rb index 4668125..6ba2f75 100644 --- a/upgrade_migrations/version_0_3_0.rb +++ b/upgrade_migrations/version_0_3_0.rb @@ -1,4 +1,4 @@ -class CreateImpressionsTable < ActiveRecord::Migration +class CreateImpressionsTable < ActiveRecord::Migration[4.2] def self.up add_column :impressions, :session_hash, :string remove_index :impressions, :name => :poly_index diff --git a/upgrade_migrations/version_0_4_0.rb b/upgrade_migrations/version_0_4_0.rb index ebd7fac..d05f6e8 100644 --- a/upgrade_migrations/version_0_4_0.rb +++ b/upgrade_migrations/version_0_4_0.rb @@ -1,4 +1,4 @@ -class Version04UpdateImpressionsTable < ActiveRecord::Migration +class Version04UpdateImpressionsTable < ActiveRecord::Migration[4.2] def self.up add_column :impressions, :referrer, :string end diff --git a/upgrade_migrations/version_1_5_2.rb b/upgrade_migrations/version_1_5_2.rb index 4f64615..457b187 100644 --- a/upgrade_migrations/version_1_5_2.rb +++ b/upgrade_migrations/version_1_5_2.rb @@ -1,4 +1,4 @@ -class Version152UpdateImpressionsTable < ActiveRecord::Migration +class Version152UpdateImpressionsTable < ActiveRecord::Migration[4.2] def self.up add_column :impressions, :params, :text