Add some stubs to fix request specs

This commit is contained in:
Eugene Burmakin 2024-04-25 22:46:20 +02:00
parent 98d33da3d1
commit 1ef2c67133
7 changed files with 51 additions and 22 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
@ -6,7 +8,7 @@ abort('The Rails environment is running in production mode!') if Rails.env.produ
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f }
# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.

View file

@ -1,13 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe "Exports", type: :request do
describe "GET /create" do
RSpec.describe 'Exports', type: :request do
describe 'GET /create' do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
sign_in create(:user)
end
it "returns http success" do
get "/export"
it 'returns http success' do
get '/export'
expect(response).to have_http_status(:success)
end
end

View file

@ -1,9 +1,14 @@
require 'rails_helper'
RSpec.describe "Homes", type: :request do
describe "GET /" do
it "returns http success" do
get "/"
RSpec.describe 'Homes', type: :request do
describe 'GET /' do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
it 'returns http success' do
get '/'
expect(response).to have_http_status(:success)
end
end

View file

@ -1,7 +1,12 @@
require 'rails_helper'
RSpec.describe "Imports", type: :request do
describe "GET /imports" do
RSpec.describe 'Imports', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'GET /imports' do
context 'when user is logged in' do
let(:user) { create(:user) }
@ -9,7 +14,7 @@ RSpec.describe "Imports", type: :request do
sign_in user
end
it "returns http success" do
it 'returns http success' do
get imports_path
expect(response).to have_http_status(200)
@ -27,7 +32,7 @@ RSpec.describe "Imports", type: :request do
end
end
describe "POST /imports" do
describe 'POST /imports' do
context 'when user is logged in' do
let(:user) { create(:user) }
let(:file) { fixture_file_upload('owntracks/export.json', 'application/json') }

View file

@ -1,13 +1,20 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe "Points", type: :request do
describe "GET /index" do
RSpec.describe 'Points', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'GET /index' do
context 'when user signed in' do
before do
sign_in create(:user)
end
it "returns http success" do
it 'returns http success' do
get points_path
expect(response).to have_http_status(:success)
@ -15,7 +22,7 @@ RSpec.describe "Points", type: :request do
end
context 'when user not signed in' do
it "returns http success" do
it 'returns http success' do
get points_path
expect(response).to have_http_status(302)

View file

@ -1,9 +1,15 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe "/stats", type: :request do
RSpec.describe '/stats', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe "GET /index" do
it "renders a successful response" do
describe 'GET /index' do
it 'renders a successful response' do
get stats_url
expect(response.status).to eq(302)
end

View file

@ -8,14 +8,13 @@ module DeviseRequestSpecHelpers
def sign_in(resource_or_scope, resource = nil)
resource ||= resource_or_scope
scope = Devise::Mapping.find_scope!(resource_or_scope)
login_as(resource, scope: scope)
login_as(resource, scope:)
end
def sign_out(resource_or_scope)
scope = Devise::Mapping.find_scope!(resource_or_scope)
logout(scope)
end
end
RSpec.configure do |config|