Ruby on Raileのユニットテスト

Ruby on Railsのユニットテストについて勉強

テストの種類とサポートツール
ユニットテストサポート
 - Test::Unit
  - Rails標準のフレームワーク - 可読性の欠ける
 - RSpec
  - 可読性に優れている - Test::Unitとは異なる独特の記法
 - Shoulde
  - Test::Utilからの移行が容易 - 機能面で、RSpecに及ばない

エンドツーエンドテスト
 - Request Spece
  - rspec-rails 標準
 - Steak
  - 受け入れテストを記述できる
 - Cucumber
  - 自然言語的な記述でテストシナリオの記述が可能

RSpecによるユニットテスト
 - リポジトリの作成 Test::Utilを組み込まない
  - reils new [APP_PATH] -T
 - rspec-rails のインストール
  - Gemfileに追記
group :development, :test do
  gem "rspec", "2.4.0"
  gem "rspec-rails", "2.4.1"
end
  - ライブラリのインストール
bundle install
 - RSpecの設定ファイル生成
rails generate rspec:install

モデルのテスト
 - モデル生成
reils generate model article title:string body:text
 - マイグレーションスクリプト実行
rake db:migrate
    - ※ undefined method `prerequisites' for nil:NilClass
      エラーの場合、rspecのバージョンを消してbundle updateを実行
group :development, :test do
  gem "rspec"
  gem "rspec-rails"
end
  - データロード
rake db:test:load

 - テストの実行
  - spec/models/article_spec.rb
require 'spec_helper'

describe Article do
  context "title and  body init" do
    before do
      @article = Article.new(
        :title => "first blog",
        :body => "first body"
      )
    end
    it "title check" do
      @article.title.should == "first blog"
    end
    it "body check" do
      @article.body.should == "first body"
    end
  end
end
  - コマンド
bundle exec rspec spec/models/article_spec.rb
 - 出力の変更
  - オプション-f dをつける
  - .rspecで設定可能
bundle exec rspec -f d spec/models/article_spec.rb

 - バリデーションのテスト
  - app/models/article.rbにバリデーションを追加
class Article > ActiveRecord::Base
  attr_accessible :body, :title
  validates :title,  :presence => true
end
  - テストの追加
  context "not title" do
    before do
      @article = Article.new
    end
    it { @article.should_not be_valid }  end
  context "exist title" do
    before do
      @article = Article.new(:title => "first blog")
    end
    it { @article.should be_valid }
  end

コントローラのテスト
 - コントローラの生成
  - rails g controller articles index
 - コントローラで使うメソッド達
  - get, post, put, delete => HTTPリクエスト発行
  - response => HTTPレスポンスの取得
  - assigns => コントローラのいんスタン変数を取得
  - flash, session, cookie => 各項目の取得
 - 一覧表示機能のテスト
  - spec/controllers/articles_controller_spec.rbの実装
require 'spec_helper'

describe ArticlesController do
  describe "GET 'index'" do
    before do
      @article1 = Article.create(
        :title => "Article 1",
        :body  => "Hello"
      )
      @article2 = Article.create(
        :title => "Article 2",
        :body  => "World"
      )
     get 'index'
    end
    it "should be success" do
      response.should be_success
    end
    it "all articles" do
      assigns[:articles].should =~ [@article1, @article2]
    end
  end
end
  - コントローラに一覧取得処理を追加
class ArticlesController < ApplicationController
  def index
   @articles = Article.all
  end
end

ビューのテスト
 - 一覧画面のテスト
require 'spec_helper'

describe "articles/index.html.erb" do
  before do
    assign(
      :articles, [
        Article.create(
          :title => "first blog",
          :body  => "content"
        )
      ]
    )
    render
  end
  it "title view" do
    rendered.should =~ /first blog/
  end
  it "body view" do
    rendered.should =~ /content/
  end
end
 - viewのテストで使うメソッド
  - assign => ビューのインスタンス変数に値を設定する
  - render => ビューのレンダリングを行う
  - rendered => レンダリングした結果を返す
 - Viewの実装
<h1>Articles#index</h1>

<% @articles.each do |article| %>
  <h2><%= article.title %></h2>
  <div><%= article.body %></div>
<% end %>

ヘルパーのテスト
 - 登校日を表示するヘルパー
require 'spec_helper'
describe ArticlesHelper do
  describe "#posted_on" do
    before do
      @now = Time.now
      @article = Article.create(
        :title => 'first blog',
        :body  => 'content'
      )
    end
    it {
      helper.posted_on(@article).should == "#{@now.year}/#{@now.month}/#{@now.day}"
    }
  end
end
 - ヘルパーのテストで使うメソッド
  - assign => インスタンス変数をセットする
  - helper => 対象ページのヘルパーをインクルードしたオブジェクトにアクセス
 - ヘルパーの実装
module ArticlesHelper
  def posted_on(article)
    time = article.created_at
    "#{time.year}/#{time.month}/#{time.day}"
  end
end


0 件のコメント:

コメントを投稿

ReactNativeでAndroid対応する話

前提 ReactNativeでiOS版のアプリをリリースしていて、Android版をリリースする話 トラブルシューティング Build.VERSION_CODES.Q が存在しないエラー compileSdkVersionを29以上にすると解決 メモリー足りないエラー Execu...