8/9の学習日記

RSpec

RSpecの基礎知識

テストとは

webアプリケーション開発において、テストは様々な意味で用いられます。
プログラマにおいての「テスト」は、専用のプログラムによってWebアプリケーションの動作を確認することです。ソフトウェアによって自動で実施されるテストという意味です。




RSpecとは

標準フレームワークはMinitestですが、別のRSpecを用います。
RSpecの初期設定

bin/rails g rspec:install
  • spec_helper.rb
  • rails_helper.rb
が生成される。
RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups

end

RSpec-はじめの一歩

テストコード
require "rspec_helper"

describe String do
    describe "#<<" do
        example "文字の追加" do
            s = "ABC"
            s << "D"
            expect(s.size).to eq(4)
        end
    end
end

エグザンプル

エグザンプルとは

        example "文字の追加" do
            s = "ABC"
            s << "D"
            expect(s.size).to eq(4)

エグザンプルグループ
describe~endまでをグループ
Stringクラスのグループ > 次は、<<メソッドのグループ
<< 文字の追加

require "rspec_helper"

describe String do
    describe "#<<" do
        example "文字の追加" do
            s = "ABC"
            s << "D"
            expect(s.size).to eq(4)
        end
    end
end

テスト結果の読み方

失敗時
.F

pendingメソッド

修正時、いったん保留したいときはpendingを使う
require "rspec_helper"

describe String do
    describe "#<<" do
        example "文字の追加" do
            pending("調査中")
            s = "ABC"
            s << "D"
            expect(s.size).to eq(4)
        end
    end
end

xexampleメソッド


require "rspec_helper"

describe String do
    describe "#<<" do
        xexample "文字の追加" do
            s = "ABC"
            s << "D"
            expect(s.size).to eq(4)
        end
    end
end
めんどくさいときは、exampleにxをつけると同じになる

expectメソッドとマッチャー

オブジェクトを対象にする場合

            expect(s.size).to eq(4)
ここの構文expect(T).to M
T..ターゲット,M..マッチャー
マッチャーは、ターゲットに指定されたオブジェクトがある条件を満たすか調べるオブジェクト

ブロックを対象にする場合

expect {...} to M
{}:ブロックでも実行が可能です。
            expect { s << nil }.to raise_error(TypeError)
raise_errorは例外が出るかどうかを確かめる

エグザンプルの絞り込み

行番号による絞り込み

rspec/experiments/string_spec.rb:11
のように特定行のみをテストできる

タグによる絞り込み

rspec spec --tag=exception
にて:exceptionタグが存在するもののみテスト可能

ビジュアルデザイン

仮設トップページの作成

ルーティングの設定

config/routes.rb
Rails.application.routes.draw do
  namespace :staff do
    root "top#index"
  end

  namespace :admin do
    root "top#index"
  end

  namespace :customer do
    root "top#index"
  end
end
namespaceメソッド
第一引数の:staffをシンボル指定、
root はアクションの指定になる
つまり、topのindexを使用(topController)

コントローラーとアクションの作成

bin/rails g controller staff/top
bin/rails g controller admin/top
bin/rails g controller customer/top
class Staff::TopController < ApplicationController
    def index
        render action: "index"
    end
end

railsのアクションとはコントローラクラスのpublicなインスタンスメソッドを指す。
render action: "index"
renderはHTML文書を生成するメソッドです。ERBというライブラリを用いて生成します。ERBで解釈可能なテキストをERBテンプレートと呼びます。
ERBテンプレートはapp/viewsディレクトリに、
左記の文の意味は、標準パスにあるindexアクション用のERBテンプレートを用いてHTML文書を作成せよという意味です。
他に指示がない場合、暗黙的に生成されるので省略可能

ERBテンプレートの作成

レイアウト

app/views/layouts/
<!DOCTYPE html>
<html>
  <head>
    <title>Baukis</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag 'application'media: 'all''data-turbolinks-track''reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>
Next Post Previous Post
No Comment
Add Comment
comment url