質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

5911閲覧

rubocopのLint/Syntax errorについて

Harluz

総合スコア19

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/11/07 06:12

railsでポートフォリオを作成中にrubocopにてコードをチェックしているが、
Espec/requests/users_requests_spec.rb:97:1: E: Lint/Syntax: unexpected token kEND
(Using Ruby 2.6 parser; configure using TargetRubyVersion parameter, under AllCops)
end
^^^
のエラーが吐かれるようになっていた。

私が疑っているもしくは分からない点について、整理します。
1.構文ミスの可能性
2.rubocop.ymlやruby-versionの設定ミス
3.指定したバージョンが採用されるようにする方法が分からない
(ネットで調べる際にうまく言語化できない)

requestspec

1# frozen_string_literal: true 2 3require 'rails_helper' 4 5RSpec.describe 'UsersRequests', type: :request do 6 before do 7 get signup_path 8 end 9 10 describe 'GET #index' 11 12 describe 'GET #show' do 13 before { get users_path, params: { user: create(:user) } } 14 subject { response } 15 16 it 'responds successfully' do 17 is_expected.to be_successful 18 end 19 20 it 'returns a 200 response' do 21 is_expected.to have_http_status(200) 22 end 23 end 24 25 describe 'GET #new' do 26 before { get new_user_path } 27 subject { response } 28 29 it 'responds successfully' do 30 is_expected.to be_successful 31 end 32 33 it 'returns a 200 response' do 34 is_expected.to have_http_status(200) 35 end 36 end 37 38 describe 'POST #create' do 39 describe 'valid request' do 40 describe 'check users count' do 41 it 'add a user' do 42 expect do 43 post signup_path, params: { user: attributes_for(:user) } 44 end.to change(User, :count).by(1) 45 end 46 end 47 48 describe 'check response' do 49 before { post users_path, params: { user: attributes_for(:user) } } 50 subject { response } 51 52 it 'rediorect to show' do 53 is_expected.to redirect_to user_path(User.last) 54 end 55 56 it 'returns a 302 response' do 57 is_expected.to have_http_status(302) 58 end 59 end 60 end 61 62 describe 'invalid request' do 63 describe 'check user count' do 64 it 'does not add a user' do 65 expect do 66 post signup_path, params: { user: attributes_for(:invalid_user) } 67 end.to change(User, :count).by(0) 68 end 69 end 70 71 describe 'check response' do 72 before { post signup_path, params: { user: attributes_for(:invalid_user) } } 73 subject { response } 74 75 it 'render new' do 76 is_expected.to render_template('new') 77 end 78 79 it 'responds successfully' do 80 is_expected.to be_successful 81 end 82 83 it 'returns a 200 response' do 84 is_expected.to have_http_status(200) 85 end 86 end 87 end 88 end 89 end 90 91 describe 'GET #edit' do end 92 93 describe 'PUT #update' do end 94 95 describe 'DELETE #destroy' do end 96 97end

構文的に間違えているようには見えず、
.rubocop.ymlのterget ruby versionでもrubyのバージョンと合わせている
.ruby-versionでも同様に使用しているrubyのバージョンと合致している。

% ruby -v ruby 2.6.3p62

rubyVersionfile

12.6.3

rubocopyml

1inherit_from: .rubocop_todo.yml 2 3AllCops: 4 Exclude: 5 - 'db/**/*' 6 - 'tmp/**/*' 7 - 'vendor/**/*' 8 - 'lib/geohash.rb' 9 - 'lib/manager/geo.rb' 10 - 'bin/spring' 11 - 'Gemfile' 12 - 'spec/*.rb' 13 - 'config/puma.rb' 14 - '.pryrc' 15 TargetRubyVersion: 2.6.3 16 17 18# Offense count: 326 19# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. 20# URISchemes: http, https 21Layout/LineLength: 22 Exclude: 23 - 'Rakefile' 24 - '**/*/.rake' 25 - 'spec/**/*.rb' 26 Max: 275 27 28Naming/PredicateName: 29 ForbiddenPrefixes: 30 - has_ 31 32Style/MixinUsage: 33 Include: 34 - app/**/*.rb 35 36Naming/MethodParameterName: 37 MinNameLength: 1 38 39Naming/AccessorMethodName: 40 Enabled: false 41 42Style/Documentation: 43 Enabled: false 44 45Style/AsciiComments: 46 Enabled: false 47 48# Offense count: 7 49Metrics/PerceivedComplexity: 50 Max: 18 51 52# Offense count: 45 53# Configuration parameters: CountComments. 54#Metrics/MethodLength: 55 #Max: 106 56 57# Offense count: 45 58# Configuration parameters: CountComments. 59#Metrics/MethodLength: 60 #Max: 109 61 62# Offense count: 1 63# Configuration parameters: CountKeywordArgs. 64Metrics/ParameterLists: 65 Max: 7 66 67# Offense count: 7 68Metrics/CyclomaticComplexity: 69 Max: 15 70 71# Offense count: 4 72# Configuration parameters: CountComments. 73Metrics/ClassLength: 74 Max: 140 75 76# Offense count: 1 77# Configuration parameters: CountBlocks. 78Metrics/BlockNesting: 79 Max: 4 80 81# Offense count: 40 82# Configuration parameters: CountComments, ExcludedMethods. 83Metrics/BlockLength: 84 Max: 111 85 86# Offense count: 53 87Metrics/AbcSize: 88 Max: 109 89 90# Offense count: 1 91Lint/SuppressedException: 92 Exclude: 93 - 'config/unicorn.rb' 94

.rubocop.ymlではrspecが対象から外れるように設定している。
しかし、なぜかrequestspecのみが警告される。

エラー文からもparserが関係しているように思ったために、gemfileにて
gem 'parser', '2.7.1.5'
でbundle update parserを実施するもエラーは解決されず、、、、
update後に下記コマンドを実行

$ bundle list | grep parser * parser (2.7.2.0) * regexp_parser (1.8.1)

parserは2.7.2.0が採用されている?

どんな些細なことでも構いませんので、ご教示していただければ助かります。
また、この他に必要なデータが必要であれば、アップします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

do endと続けて書くことはできません。do; endとセミコロンを入れるか、あるいは空の波カッコで書いてください。本当に文法エラーです。

投稿2020/11/07 06:49

maisumakun

総合スコア145184

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Harluz

2020/11/07 07:01

do;l endを試して見ても解決しませんでした。 思い切って、不要なコードをコメントアウトし、最終行のendもコメントアウトした状態でrubocop -a(自動修正コマンド)を実行した結果、直りました。
guest

0

自己解決

request

1# frozen_string_literal: true 2 3require 'rails_helper' 4 5RSpec.describe 'UsersRequests', type: :request do 6 before do 7 get signup_path 8 end 9 10 # describe 'GET #index' do; end 11 12 describe 'GET #show' do 13 before { get users_path, params: { user: create(:user) } } 14 subject { response } 15 16 it 'responds successfully' do 17 is_expected.to be_successful 18 end 19 20 it 'returns a 200 response' do 21 is_expected.to have_http_status(200) 22 end 23 end 24 25 describe 'GET #new' do 26 before { get new_user_path } 27 subject { response } 28 29 it 'responds successfully' do 30 is_expected.to be_successful 31 end 32 33 it 'returns a 200 response' do 34 is_expected.to have_http_status(200) 35 end 36 end 37 38 describe 'POST #create' do 39 describe 'valid request' do 40 describe 'check users count' do 41 it 'add a user' do 42 expect do 43 post signup_path, params: { user: attributes_for(:user) } 44 end.to change(User, :count).by(1) 45 end 46 end 47 48 describe 'check response' do 49 before { post users_path, params: { user: attributes_for(:user) } } 50 subject { response } 51 52 it 'rediorect to show' do 53 is_expected.to redirect_to user_path(User.last) 54 end 55 56 it 'returns a 302 response' do 57 is_expected.to have_http_status(302) 58 end 59 end 60 end 61 62 describe 'invalid request' do 63 describe 'check user count' do 64 it 'does not add a user' do 65 expect do 66 post signup_path, params: { user: attributes_for(:invalid_user) } 67 end.to change(User, :count).by(0) 68 end 69 end 70 71 describe 'check response' do 72 before { post signup_path, params: { user: attributes_for(:invalid_user) } } 73 subject { response } 74 75 it 'render new' do 76 is_expected.to render_template('new') 77 end 78 79 it 'responds successfully' do 80 is_expected.to be_successful 81 end 82 83 it 'returns a 200 response' do 84 is_expected.to have_http_status(200) 85 end 86 end 87 end 88 end 89end 90 91# describe 'GET #edit' do; end 92 93# describe 'PUT #update' do; end 94 95# describe 'DELETE #destroy' do; end 96 97# end 98

テストコードを記述していないものと最終行のendをコメントアウトした状態でrubocop -aコマンドを実行した結果、上記の通りとなりエラーは消えました。

投稿2020/11/07 07:03

Harluz

総合スコア19

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問