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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

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

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

Q&A

解決済

1回答

2048閲覧

Rails5.1.3のRspecテストででたえらーの内容がわかりません。

koume

総合スコア458

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

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

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

0グッド

0クリップ

投稿2017/08/24 15:25

RailsでRspecのテストを実行したら意味不明なエラーが出たのでどなたか読み解いてくれないでしょうか?エラーの解消方法が
わからないのでお願いします。

[vagrant@localhost chibi]$ bin/rspec spec/controllers/staff/top_controller_spec.rb Running via Spring preloader in process 15544 FF... Failures: 1) Staff::TopController ログイン前 IPアドレスによるアクセス制限 許可 Failure/Error: where(namespace: namespace).where(options_for(ip_address)).exists? ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual tha t corresponds to your MySQL server version for the right syntax to use near '.gs ub(/ +/, ' ').strip) LIMIT 1' at line 4: SELECT 1 AS one FROM `allowed_sources` WHERE `allowed_sources`.`namespace` = 'staff' AND ( octet1 = '0' AND octet2 = '0' AND octet3 = '0' AND ((octet4 = '0' AND wildcard = 0) OR wildcard = 1) .gsub(/ +/, ' ').strip) LIMIT 1 # ./app/models/allowed_source.rb:48:in `include?' # ./app/controllers/staff/base.rb:18:in `check_source_ip_address' # ./spec/controllers/staff/top_controller_spec.rb:13:in `block (3 levels) i n <top (required)>' # -e:1:in `<main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.gsub(/ +/, ' ') .strip) LIMIT 1' at line 4 # ./app/models/allowed_source.rb:48:in `include?' 2)も同じ内容です。

Rspecによるテストコード

spec/controllers/staff/top_controller_spec.rb require 'rails_helper' describe Staff::TopController, 'ログイン前' do let(:staff_member) { create(:staff_member) } describe 'IPアドレスによるアクセス制限' do before do Rails.application.config.chibi[:restrict_ip_addresses] = true end example '許可' do AllowedSource.create!(namespace: 'staff', ip_address: '0.0.0.0') get :index expect(response).to render_template('staff/top/index') end example '拒否' do AllowedSource.create!(namespace: 'staff', ip_address: '192.168.0.*') get :index expect(response).to render_template('errors/forbidden') end end end describe Staff::TopController, 'ログイン後' do let(:staff_member) { create(:staff_member) } before do session[:staff_member_id] = staff_member.id session[:last_access_time] = 1.second.ago end describe '#index' do example '通常はstaff/top/dashboardを表示' do get :index expect(response).to render_template('staff/top/dashboard') end example '停止フラグがセットされたら強制的にログアウト' do staff_member.update_column(:suspended, true) get :index expect(session[:staff_member_id]).to be_nil expect(response).to redirect_to(staff_root_url) end example 'セッションタイムアウト' do session[:last_access_time] = Staff::Base::TIMEOUT.ago.advance(seconds: -1) get :index expect(session[:staff_member_id]).to be_nil expect(response).to redirect_to(staff_login_url) end end end

失敗したと思われるコード

class AllowedSource < ActiveRecord::Base attr_accessor :last_octet, :_destroy before_validation do if last_octet self.last_octet.strip! if last_octet == '*' self.octet4 = 0 self.wildcard = true else self.octet4 = last_octet end end end validates :octet1, :octet2, :octet3, :octet4, presence: true, numericality: { only_integer: true, allow_blank: true }, inclusion: { in: 0..255, allow_blank: true } validates :octet4, uniqueness: { scope: [ :octet1, :octet2, :octet3 ], allow_blank: true } validates :last_octet, inclusion: { in: (0..255).to_a.map(&:to_s) + [ '*' ], allow_blank: true } after_validation do if last_octet errors[:octet4].each do |message| errors.add(:last_octet, message) end end end def ip_address=(ip_address) octets = ip_address.split('.') self.octet1 = octets[0] self.octet2 = octets[1] self.octet3 = octets[2] if octets[3] == '*' self.octet4 = 0 self.wildcard = true else self.octet4 = octets[3] end end class << self def include?(namespace, ip_address) !Rails.application.config.chibi[:restrict_ip_addresses] || where(namespace: namespace).where(options_for(ip_address)).exists? #このあたりでエラーがでています。 end private def options_for(ip_address) octets = ip_address.split('.') condition = %Q{ octet1 = ? AND octet2 = ? AND octet3 = ? AND ((octet4 = ? AND wildcard = ?) OR wildcard = ?) #このあたりのコードがダメなのでは?と思います。 .gsub(/\s+/, ' ').strip} [ condition, *octets, false, true ] end end end

以上宜しくお願いします。

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

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

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

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

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

guest

回答1

0

自己解決

.gsub(/\s+/, ' ').strip}の部分を}.gsub(/\s+/, ' ').stripにしたらテストは成功しました。

投稿2017/08/25 00:58

koume

総合スコア458

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問