You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert
これはなに RSpecを利用したコントローラの機能テストは、Rails4まではcontroller specで行われて来ました。しかしRails5からはrequest specで記述することが推奨され、assignsとassert_templateの使用が非推奨となりました。 rails-controller-testinggemを使用すればassignsやassert_templateを使うことはできますが、やはりrequest specへ移行することが望ましいと考えられています。 これから新しく作成する Rails アプリケーションについては、 rails-controller-testing gem を追加するのはおすすめしません。 Rails チームや RSpec コアチームとしては、代わりに request spec を書くことを推奨します。 RSpec 3.5 がリリースされま
外部API呼び出しのあるアプリケーションを開発する場合、テストやローカル環境での動作確認まで本物のAPIを呼び出していると、時間がかかり開発の生産性が下がるのでAPIモックを使いたくなります。 そこでWebMockというGemを使うと、外部へのHTTPリクエストをスタブ化してくれるので、開発が非常にやりやすくなります。 今回はRailsアプリケーションでのWebMockの使い方を説明します。 インストール方法 Gemfileに書いてbundlerでインストールします。 gem 'webmock' bundle install --path vendor/bundle 設定方法 事前に自分の欲しいレスポンスを返却できるようにスタブを登録します。 (ここではレスポンスデータを事前に作成して、そのファイルを読み込むように指定しています) require 'webmock' WebMock.ena
An RSpec cheatsheet in the form of a Rails app. Learn how to expertly test Rails apps from a model codebase A small yet comprehensive reference for developers who want to know how to test Rails apps using RSpec. Here you'll find in-depth examples with detailed documentation explaining how to test with RSpec and related testing gems, which you can then apply to your own projects. This application w
Railsの認証系の定番ライブラリdeviseでコントローラをテストする場合、 ログインした状態のテストをしたい場合の対処方法を確立した。 公式のが一番確実だけれども、公式の方法だと作ったユーザオブジェクトの属性を変更することが出来ない。 例) 有料ユーザや無料ユーザの振る舞いや、退会ユーザの場合など 参考: How To: Test controllers with Rails 3 and 4 (and RSpec) https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec) そこで上記リンクのものを修正/拡張した ログイン用のモジュールを作る spec/support/controller_macros.rb にログイン用のモジュールを作る co
I’m frequently asked what it takes to begin testing Rails applications. The hardest part of being a beginner is that you often don’t know the terminology or what questions you should be asking. What follows is a high-level overview of the tools we use, why we use them, and some tips to keep in mind as you are starting out. RSpec We use RSpec over Test::Unit because the syntax encourages human read
この版のまえがき 改訂版の「Everyday Rails - RSpecによるRailsテスト入門」を手にとっていただき、どうもありがとうございます。改訂版をリリースするまで、長い時間がかかりました。そして、内容も大きく変わりました。本書を読んだみなさんに「長い間待った甲斐があった」と思っていただけると幸いです。 なぜこんなに時間がかかったのでしょうか?前述のとおり、内容は大きく変わりました。本の内容そのものも変わりましたし、Railsにおける一般的なテストの考え方も変わっています。まず後者について説明しましょう。Rails 5.0の登場と同時に、Railsチームはコントローラのテストを事実上非推奨としました。個人的にこれは素晴らしいニュースでした。本書の前の版では説明に 3章 も使っていましたが、私も最初はコントローラのテストを理解するのに非常に苦労したのを思い出しました。そして、最近で
Railsのテストで複数のオブジェクトの作成を簡易に行えるFactoryGirl。 FactoryGirlについて基本的なことを知っていることを前提に、RailsでFactoryGirlを使うよく使う機能やTipsをまとめました。 動作確認 Rails 4.1.7 Factory Girl 4.4.0 Factory Girl Rails 4.4.1 目次 1. Factory Girlのインストール 2. FactoryGirlシンタックスの省略 3. FactoryGirlの使い方あれこれ 3.1. オブジェクトのビルド、作成、スタブ作成、属性取得 3.2. 特定の値を指定してオブジェクトを作成 3.3. ブロックを渡すことで細かな処理を記載可能 3.4. 一度に複数のレコードを作成する 4. Factory定義のあれこれ 4.1. 他の属性に依存する属性を定義する 4.2. fact
はじめに RSpecを使ってテストを記述している際、テストの実行前にデータをテーブルに登録しておきたいケースが多々あるかと思います。RSpec内でActiveRecordを使ってデータを登録することもできますが、複数のテストケースで同じデータを使いたい場合、データの定義は一箇所で行いたいところです。 この様な場合、Factory Girlを使用すると、一箇所でテストデータを定義できます。今回はこのFactoryGirlの使い方について書きたいと思います。 使い方 使い方の大まかな流れとしては、 FactoryGirlが使用できるようにする 定義ファイルにデータを定義する 必要とするテストケースにてファイルを読み込み、データを適時加工して登録する という感じとなります。尚、この定義したデータを「Factory」とも言います。以下、手順です。 1.Gemfile Gemfileに以下を記述し、
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く