6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails】setupメソッド

Posted at

#setupメソッド
テストファイル上で記載するメソッドで、各テストが走る前に実行されるメソッド。
またsetupメソッドでインスタンス変数を定義すれば全てのテスト内で、そのインスタンス変数が使用可能になります。

##実行例

test/models/user_test.rb
require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = User.new(name: "Example User", email: "user@example.com")
  end

  test "should be valid" do
    assert @user.valid?
  end
end

こうすることでテストであるshould be validが走る直前にset upが実行されるようになります。
assert @user.valid?@userset upから持ってきています。

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?