Skip to content

Commit a574227

Browse files
authored
Merge pull request #6 from 0rientd/development
Created test in rspec
2 parents 5513bfb + dcc955a commit a574227

7 files changed

Lines changed: 295 additions & 0 deletions

File tree

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ group :development, :test do
4545

4646
# Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
4747
gem "rubocop-rails-omakase", require: false
48+
49+
gem "rspec-rails", "~> 8.0"
4850
end
4951

5052
group :development do

Gemfile.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ GEM
101101
debug (1.11.0)
102102
irb (~> 1.10)
103103
reline (>= 0.3.8)
104+
diff-lcs (1.6.2)
104105
drb (2.2.3)
105106
erb (5.0.2)
106107
erubi (1.13.1)
@@ -225,6 +226,27 @@ GEM
225226
reline (0.6.2)
226227
io-console (~> 0.5)
227228
rexml (3.4.1)
229+
rspec (3.13.1)
230+
rspec-core (~> 3.13.0)
231+
rspec-expectations (~> 3.13.0)
232+
rspec-mocks (~> 3.13.0)
233+
rspec-core (3.13.5)
234+
rspec-support (~> 3.13.0)
235+
rspec-expectations (3.13.5)
236+
diff-lcs (>= 1.2.0, < 2.0)
237+
rspec-support (~> 3.13.0)
238+
rspec-mocks (3.13.5)
239+
diff-lcs (>= 1.2.0, < 2.0)
240+
rspec-support (~> 3.13.0)
241+
rspec-rails (8.0.1)
242+
actionpack (>= 7.2)
243+
activesupport (>= 7.2)
244+
railties (>= 7.2)
245+
rspec-core (~> 3.13)
246+
rspec-expectations (~> 3.13)
247+
rspec-mocks (~> 3.13)
248+
rspec-support (~> 3.13)
249+
rspec-support (3.13.4)
228250
rubocop (1.78.0)
229251
json (~> 2.3)
230252
language_server-protocol (~> 3.17.0.2)
@@ -339,6 +361,8 @@ DEPENDENCIES
339361
pg (~> 1.5)
340362
puma (>= 5.0)
341363
rails (~> 7.2.2, >= 7.2.2.1)
364+
rspec (~> 3.13)
365+
rspec-rails (~> 8.0)
342366
rubocop-rails-omakase
343367
selenium-webdriver
344368
sprockets-rails
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe SearchQueriesHelper, type: :helper do
4+
describe 'all_trending_queries' do
5+
it 'return the first 3 terms in desc order' do
6+
SearchQuery.create!(term: "term1", ip_address: '127.0.0.1')
7+
SearchQuery.create!(term: "term1", ip_address: '127.0.0.1')
8+
SearchQuery.create!(term: "term1", ip_address: '127.0.0.1')
9+
SearchQuery.create!(term: "term2", ip_address: '127.0.0.1')
10+
SearchQuery.create!(term: "term2", ip_address: '127.0.0.1')
11+
SearchQuery.create!(term: "term3", ip_address: '127.0.0.1')
12+
13+
result = helper.all_trending_queries
14+
15+
expect(result.keys.size).to eq(3)
16+
expect(result.keys.first).to eq('term1')
17+
end
18+
end
19+
20+
describe 'format_trend_information' do
21+
it 'format the info' do
22+
trend = ['Fortnite', 10]
23+
expect(helper.format_trend_information(trend)).to eq('Fortnite (10 hits)')
24+
end
25+
26+
it 'return nil if empty' do
27+
expect(helper.format_trend_information(nil)).to be_nil
28+
expect(helper.format_trend_information([])).to be_nil
29+
end
30+
end
31+
32+
describe 'format_analytics_information' do
33+
it 'format the analytic info' do
34+
analytic = SearchQuery.new(term: 'Fortnite', ip_address: '192.168.0.1')
35+
expect(helper.format_analytics_information(analytic)).to eq('Term: Fortnite | (From 192.168.0.1)')
36+
end
37+
38+
it 'return nil if empty' do
39+
expect(helper.format_analytics_information(nil)).to be_nil
40+
end
41+
end
42+
43+
describe 'format_top_ten_information' do
44+
it 'format the top ten info' do
45+
analytic = ['Minecraft', 20]
46+
expect(helper.format_top_ten_information(analytic)).to eq('Term: Minecraft (20 hits)')
47+
end
48+
49+
it 'return nil if empty' do
50+
expect(helper.format_top_ten_information(nil)).to be_nil
51+
end
52+
end
53+
end

spec/rails_helper.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# This file is copied to spec/ when you run 'rails generate rspec:install'
2+
require 'spec_helper'
3+
ENV['RAILS_ENV'] ||= 'test'
4+
require_relative '../config/environment'
5+
# Prevent database truncation if the environment is production
6+
abort("The Rails environment is running in production mode!") if Rails.env.production?
7+
# Uncomment the line below in case you have `--require rails_helper` in the `.rspec` file
8+
# that will avoid rails generators crashing because migrations haven't been run yet
9+
# return unless Rails.env.test?
10+
require 'rspec/rails'
11+
# Add additional requires below this line. Rails is not loaded until this point!
12+
13+
# Requires supporting ruby files with custom matchers and macros, etc, in
14+
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
15+
# run as spec files by default. This means that files in spec/support that end
16+
# in _spec.rb will both be required and run as specs, causing the specs to be
17+
# run twice. It is recommended that you do not name files matching this glob to
18+
# end with _spec.rb. You can configure this pattern with the --pattern
19+
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
20+
#
21+
# The following line is provided for convenience purposes. It has the downside
22+
# of increasing the boot-up time by auto-requiring all files in the support
23+
# directory. Alternatively, in the individual `*_spec.rb` files, manually
24+
# require only the support files necessary.
25+
#
26+
# Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).each { |f| require f }
27+
28+
# Ensures that the test database schema matches the current schema file.
29+
# If there are pending migrations it will invoke `db:test:prepare` to
30+
# recreate the test database by loading the schema.
31+
# If you are not using ActiveRecord, you can remove these lines.
32+
begin
33+
ActiveRecord::Migration.maintain_test_schema!
34+
rescue ActiveRecord::PendingMigrationError => e
35+
abort e.to_s.strip
36+
end
37+
RSpec.configure do |config|
38+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
39+
config.fixture_paths = [
40+
Rails.root.join('spec/fixtures')
41+
]
42+
43+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
44+
# examples within a transaction, remove the following line or assign false
45+
# instead of true.
46+
config.use_transactional_fixtures = true
47+
48+
# You can uncomment this line to turn off ActiveRecord support entirely.
49+
# config.use_active_record = false
50+
51+
# RSpec Rails uses metadata to mix in different behaviours to your tests,
52+
# for example enabling you to call `get` and `post` in request specs. e.g.:
53+
#
54+
# RSpec.describe UsersController, type: :request do
55+
# # ...
56+
# end
57+
#
58+
# The different available types are documented in the features, such as in
59+
# https://rspec.info/features/8-0/rspec-rails
60+
#
61+
# You can also this infer these behaviours automatically by location, e.g.
62+
# /spec/models would pull in the same behaviour as `type: :model` but this
63+
# behaviour is considered legacy and will be removed in a future version.
64+
#
65+
# To enable this behaviour uncomment the line below.
66+
# config.infer_spec_type_from_file_location!
67+
68+
# Filter lines from Rails gems in backtraces.
69+
config.filter_rails_from_backtrace!
70+
# arbitrary gems may also be filtered via:
71+
# config.filter_gems_from_backtrace("gem name")
72+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe SearchQueriesController, type: :request do
4+
describe 'POST /search_queries' do
5+
let!(:article) { Article.create!(title: "Brazil") }
6+
7+
context 'with turbo_stream in post request' do
8+
context 'with no term in params' do
9+
it "grant the response there's turbo in response" do
10+
post search_queries_path, params: { search_query: { term: "" } }, as: :turbo_stream
11+
12+
expect(response).to have_http_status(:ok)
13+
expect(response.body).to include("turbo-stream")
14+
end
15+
end
16+
17+
context 'with a term with the same name with article title' do
18+
it "grant the response there's turbo in response and save the search" do
19+
expect { post search_queries_path, params: { search_query: { term: "Brazil" } }, as: :turbo_stream }.to change(SearchQuery, :count).by(1)
20+
21+
expect(response.body).to include("last_queries")
22+
expect(response.body).to include("sub_menu")
23+
expect(response.body).to include("turbo-stream")
24+
end
25+
end
26+
27+
context 'with a term different from article' do
28+
it "grant the response there's turbo in response and save the search" do
29+
post search_queries_path, params: { search_query: { term: "Space" } }, as: :turbo_stream
30+
31+
expect(response.body).to include("last_queries")
32+
expect(response.body).to include("turbo-stream")
33+
expect(response.body).to include("sub_menu")
34+
end
35+
end
36+
end
37+
38+
# context 'with no turbo_stream in post request' do
39+
# context 'with no term in params' do
40+
# it "grant there's no turbo in response" do
41+
# post search_queries_path, params: { search_query: { term: "" } }
42+
43+
# expect(response).to_not have_http_status(:ok)
44+
# expect(response.body).to_not include("turbo-stream")
45+
# end
46+
# end
47+
# end
48+
end
49+
end

spec/spec_helper.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# The generated `.rspec` file contains `--require spec_helper` which will cause
4+
# this file to always be loaded, without a need to explicitly require it in any
5+
# files.
6+
#
7+
# Given that it is always loaded, you are encouraged to keep this file as
8+
# light-weight as possible. Requiring heavyweight dependencies from this file
9+
# will add to the boot time of your test suite on EVERY test run, even for an
10+
# individual file that may not need all of that loaded. Instead, consider making
11+
# a separate helper file that requires the additional dependencies and performs
12+
# the additional setup, and require it from the spec files that actually need
13+
# it.
14+
#
15+
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16+
RSpec.configure do |config|
17+
# rspec-expectations config goes here. You can use an alternate
18+
# assertion/expectation library such as wrong or the stdlib/minitest
19+
# assertions if you prefer.
20+
config.expect_with :rspec do |expectations|
21+
# This option will default to `true` in RSpec 4. It makes the `description`
22+
# and `failure_message` of custom matchers include text for helper methods
23+
# defined using `chain`, e.g.:
24+
# be_bigger_than(2).and_smaller_than(4).description
25+
# # => "be bigger than 2 and smaller than 4"
26+
# ...rather than:
27+
# # => "be bigger than 2"
28+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29+
end
30+
31+
# rspec-mocks config goes here. You can use an alternate test double
32+
# library (such as bogus or mocha) by changing the `mock_with` option here.
33+
config.mock_with :rspec do |mocks|
34+
# Prevents you from mocking or stubbing a method that does not exist on
35+
# a real object. This is generally recommended, and will default to
36+
# `true` in RSpec 4.
37+
mocks.verify_partial_doubles = true
38+
end
39+
40+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41+
# have no way to turn it off -- the option exists only for backwards
42+
# compatibility in RSpec 3). It causes shared context metadata to be
43+
# inherited by the metadata hash of host groups and examples, rather than
44+
# triggering implicit auto-inclusion in groups with matching metadata.
45+
config.shared_context_metadata_behavior = :apply_to_host_groups
46+
47+
# The settings below are suggested to provide a good initial experience
48+
# with RSpec, but feel free to customize to your heart's content.
49+
=begin
50+
# This allows you to limit a spec run to individual examples or groups
51+
# you care about by tagging them with `:focus` metadata. When nothing
52+
# is tagged with `:focus`, all examples get run. RSpec also provides
53+
# aliases for `it`, `describe`, and `context` that include `:focus`
54+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55+
config.filter_run_when_matching :focus
56+
57+
# Allows RSpec to persist some state between runs in order to support
58+
# the `--only-failures` and `--next-failure` CLI options. We recommend
59+
# you configure your source control system to ignore this file.
60+
config.example_status_persistence_file_path = "spec/examples.txt"
61+
62+
# Limits the available syntax to the non-monkey patched syntax that is
63+
# recommended. For more details, see:
64+
# https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
65+
config.disable_monkey_patching!
66+
67+
# Many RSpec users commonly either run the entire suite or an individual
68+
# file, and it's useful to allow more verbose output when running an
69+
# individual spec file.
70+
if config.files_to_run.one?
71+
# Use the documentation formatter for detailed output,
72+
# unless a formatter has already been configured
73+
# (e.g. via a command-line flag).
74+
config.default_formatter = "doc"
75+
end
76+
77+
# Print the 10 slowest examples and example groups at the
78+
# end of the spec run, to help surface which specs are running
79+
# particularly slow.
80+
config.profile_examples = 10
81+
82+
# Run specs in random order to surface order dependencies. If you find an
83+
# order dependency and want to debug it, you can fix the order by providing
84+
# the seed, which is printed after each run.
85+
# --seed 1234
86+
config.order = :random
87+
88+
# Seed global randomization in this process using the `--seed` CLI option.
89+
# Setting this allows you to use `--seed` to deterministically reproduce
90+
# test failures related to randomization by passing the same `--seed` value
91+
# as the one that triggered the failure.
92+
Kernel.srand config.seed
93+
=end
94+
end

0 commit comments

Comments
 (0)