Rspec勉強会
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 第2章 RSpec のセットアップ **この章の主な流れ** - budnelを使ってRspecのインストール - テスト用のデーターベースを作成 - テストしたい項目をテストできるようにRspecを設定 - 新しい機能を追加するとにテスト用のデータファイルを自動で生成できるようにアプリケーション側で設定 </br> ## 1.bundleインストールを行う ```ruby= group :development, :test do gem 'rspec-rails' #開発とテスト環境だけのインストール end bundle install ``` </br> rspec-railsがやってくれること - rspec-coreその他の独立した 必要なgemとかをインストールしてくれる - rails向けの便利な機能を提供してくれる ## 2.テスト用データベースの準備 config/database.yml ファイルの中身 ```ruby= test: <<: *default database: db/test.sqlite3 #MySQL や PostgreSQL を使っている場合 test: <<: *default database: projects_test ``` コードが見つからない場合はconfig/database.ymlに上記の記載を行いrake タスクを実行して接続可能なデータベースを作成を行う ```ruby= bin/rails db:create:all ``` </br> **rake タスクってなに?** [railsガイド](https://railsguides.jp/rails_on_rack.html#%E5%8F%82%E8%80%83%E8%B3%87%E6%96%99:~:text=Rack%E3%81%AF%E3%80%81Ruby%E3%81%AEWeb%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%81%AB%E5%AF%BE%E3%81%97%E3%81%A6%E3%80%81%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%E5%8C%96%E3%81%95%E3%82%8C%E3%81%9F%E6%9C%80%E5%B0%8F%E9%99%90%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%BF%E3%83%BC%E3%83%95%E3%82%A7%E3%82%A4%E3%82%B9%E3%82%92%E6%8F%90%E4%BE%9B%E3%81%97%E3%81%A6%E3%80%81%E3%82%A4%E3%83%B3%E3%82%BF%E3%83%BC%E3%83%95%E3%82%A7%E3%82%A4%E3%82%B9%E3%82%92%E5%BA%83%E7%AF%84%E5%9B%B2%E3%81%AB%E4%BD%BF%E3%81%88%E3%82%8B%E3%82%88%E3%81%86%E3%81%AB%E3%81%97%E3%81%BE%E3%81%99%E3%80%82Rack%E3%81%AFHTTP%E3%83%AA%E3%82%AF%E3%82%A8%E3%82%B9%E3%83%88%E3%81%A8%E3%83%AC%E3%82%B9%E3%83%9D%E3%83%B3%E3%82%B9%E3%82%92%E5%8F%AF%E8%83%BD%E3%81%AA%E3%81%8B%E3%81%8E%E3%82%8A%E7%B0%A1%E5%8D%98%E3%81%AA%E6%96%B9%E6%B3%95%E3%81%A7%E3%83%A9%E3%83%83%E3%83%94%E3%83%B3%E3%82%B0%E3%81%99%E3%82%8B%E3%81%93%E3%81%A8%E3%81%A7%E3%80%81Web%E3%82%B5%E3%83%BC%E3%83%90%E3%83%BC%E3%80%81Web%E3%83%95%E3%83%AC%E3%83%BC%E3%83%A0%E3%83%AF%E3%83%BC%E3%82%AF%E3%80%81%E3%81%9D%E3%81%AE%E9%96%93%E3%81%AB%E4%BD%8D%E7%BD%AE%E3%81%99%E3%82%8B%E3%82%BD%E3%83%95%E3%83%88%E3%82%A6%E3%82%A7%E3%82%A2%EF%BC%88%E3%83%9F%E3%83%89%E3%83%AB%E3%82%A6%E3%82%A7%E3%82%A2%E3%81%A8%E5%91%BC%E3%81%B0%E3%82%8C%E3%81%A6%E3%81%84%E3%81%BE%E3%81%99%EF%BC%89%E3%81%AEAPI%E3%82%921%E3%81%A4%E3%81%AE%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%E5%91%BC%E3%81%B3%E5%87%BA%E3%81%97%E3%81%AE%E5%BD%A2%E3%81%AB%E3%81%BE%E3%81%A8%E3%82%81%E3%81%BE%E3%81%99%E3%80%82) >Rackは、RubyのWebアプリケーションに対して、モジュール化された最小限のインターフェイスを提供して、インターフェイスを広範囲に使えるようにします。RackはHTTPリクエストとレスポンスを可能なかぎり簡単な方法でラッピングすることで、Webサーバー、Webフレームワーク、その間に位置するソフトウェア(ミドルウェアと呼ばれています)のAPIを1つのメソッド呼び出しの形にまとめます。 </br> [rakeタスクわかりやすく](https://zenn.dev/yusuke_docha/articles/2d2cfd1030f6ac#whenever%E3%81%A7%E5%AE%9A%E6%9C%9F%E7%9A%84%E3%81%AA%E5%87%A6%E7%90%86%E3%82%92%E8%A1%8C%E3%81%86) >Railsで定期的に実行したい処理をRubyコードでタスクとして定義しておき、必要なときに呼び出して実行することができる機能 特徴としてアプリケーションを起動せずにターミナル上で実行でき、サーバを起動せずに定義した処理を実行できる バックラウンドでタスクの実行にはwheneverというgemを使用する。 </br> **db:create:allコマンド** database.ymlに定義してあるデータベースを全て作成 [qiitaより](https://qiita.com/parsetree/items/e9b08c6b11f762b949de) 逆にすべての環境のDPを削除したい時 ```ruby= rake db:drop:all ``` ## 3.RSpec の設定 Rspecファイル群のインストール ```ruby= bin/rails generate rspec:install create .rspec create spec create spec/spec_helper.rb create spec/rails_helper.rb ``` 生成されるファイル - .rspec 設定ファイル ```ruby= --format documentation ``` **--format documentation** テスト結果の出力を変える ```ruby= #format documentation記載なし ..... Finished in 0.40011 seconds (files took 4.53 seconds to load) 5 examples, 0 failures #format documentation記載あり  Post バリデーションの確認 titalカラム タイトルが空または3文字以内の場合postが作成されないこと タイトルが30文字以上の場合はpostが作成されないこと 日付に関するバリデーション 開始日が今日より過去の場合postが作成されないこと 終了日が開始日より過去の場合postが作成されない事 memoカラム メモが30文字以上の場合はpostが作成されないこと Finished in 0.25123 seconds (files took 2 seconds to load) 5 examples, 0 failures ``` どのテストで??どこのテストが通ったのかがわかりやすい - spec_helper.rb、rails_helper.rb テストで使用する設定ファイルの記載を行う場所 インストールができている場合 ```ruby= bundle exec rspec No examples found. Finished in 0.00047 seconds (files took 0.11704 seconds to load) 0 examples, 0 failures ``` **binstubを使ってコマンドを短くすることができる。** 何かしらのRailsアプリケーションを実行するときはbundleを使用が必須であり実行コマンドがどうしても長くなってしまうRspecの場合はbudle exec rpsecになる 少しでもコマンドを短くする方法として<span style="color: red; ">binstub</span>コマンドを使用する。 インストール後、RubyGems は実行可能ファイルと共に以下のものを用意してくれる ```ruby= bundle binstubs rspec-core コマンド実行後 ↓↓↓↓ #コマンド実行可能 bin/rspec #binディレクトリー内にrsepcの実行ファイルが生成される。 bin/ ├── bundle ├── importmap ├── rails ├── rake ├── rspec #作成される └── setup ``` [binstubについて](https://qiita.com/tanaka51/items/bc22c1c364202d3cce4e) ## 4.ジェネレーターの設定について Rspecを導入済みなのでジェネレーターのコマンドを使用してもminitestのファイルは作成されない代わりに、Rspecのファイルがspecディレクトリーに作成されて不必要なファイルも作成されてしまうここでは最初から作られないようにどのファイルを作成しないかを決めることができる。 falseを記載すれば作られない config/application.rb ```ruby= class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 7.0 config.generators do |g| g.test_framework :rspec, fixtures: false, view_specs: false, end end ``` テスト以外のファイルについて ヘルパー、jsなどいらないファイル名の記載することがで 作成をスキップすることができる。 ``` config.generators do |g| g.stylesheets false g.javascripts false g.helper false ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully