Tag: Railsテスティングガイド
$ ls -F test controllers/ helpers/ mailers/ system/ test_helper.rb fixtures/ integration/ models/ application_system_test_case.rb
$ bin/rails generate model article title:string body:text ... create app/models/article.rb create test/models/article_test.rb create test/fixtures/articles.yml ...
require 'test_helper' class ArticleTest < ActiveSupport::TestCase # test "the truth" do # assert true # end end
test "the truth" do assert true end def test_the_truth assert true end
test "should not save article without title" do article = Article.new assert_not article.save end
$ bin/rails test test/models/article_test.rb
class Article < ApplicationRecord validates :title, presence: true end
$ bin/rails test test/models/article_test.rb $ bin/rails test test/models/article_test.rb -n test_the_truth $ bin/rails test test/models/article_test.rb:6 #行番号の指定 $ bin/rails test test/controllers #ディレクトリ
# lo & behold! I am a YAML comment! david: name: David Heinemeier Hansson birthday: 1979-10-15 profession: Systems development steve: name: Steve Ross Kellock birthday: 1974-09-27 profession: guy with keyboard
# In fixtures/categories.yml about: name: About # In fixtures/articles.yml first: title: Welcome to Rails! body: Hello world! category: about
# this will return the User object for the fixture named david users(:david) # this will return the property for david called id users(:david).id # one can also access methods available on the User class david = users(:david) david.call(david.partner)
$ bin/rails generate test_unit:model article title:string body:text create test/models/article_test.rb create test/fixtures/articles.yml
$ bin/rails generate system_test users
require "application_system_test_case" class UsersTest < ApplicationSystemTestCase # test "visiting the index" do # visit users_url # # assert_selector "h1", text: "Users" # end end
require "test_helper" require "capybara/poltergeist" class ApplicationSystemTestCase < ActionDispatch::SystemTestCase driven_by :poltergeist end class ApplicationSystemTestCase < ActionDispatch::SystemTestCase driven_by :selenium, using: :firefox end class ApplicationSystemTestCase < ActionDispatch::SystemTestCase driven_by :selenium, using: :headless_chrome end
$ bin/rails generate system_test articles
require "application_system_test_case" class ArticlesTest < ApplicationSystemTestCase test "viewing the index" do visit articles_path assert_selector "h1", text: "Articles" end end
bin/rails test:system
★chromedriverが存在しないと実行できない。macOSの場合「brew cask install chromedriver」でインストール可能。
test "creating an article" do visit articles_path click_on "New Article" fill_in "Title", with: "Creating an Article" fill_in "Body", with: "Created this article successfully!" click_on "Create Article" assert_text "Creating an Article" end
$ bin/rails generate integration_test user_flows
require 'test_helper' class UserFlowsTest < ActionDispatch::IntegrationTest # test "the truth" do # assert true # end end
$ bin/rails generate integration_test blog_flow
$ ls -F test controllers/ helpers/ mailers/ test_helper.rb fixtures/ integration/ models/
david: name: David Heinemeier Hansson birthday: 1979-10-15 profession: Systems development steve: name: Steve Ross Kellock birthday: 1974-09-27 profession: guy with keyboard
# davidという名前のフィクスチャに対応するUserオブジェクトを返す users(:david) # idで呼び出されたdavidのプロパティを返す users(:david).id # Userクラスで利用可能なメソッドにアクセスすることもできる email(david.girlfriend.email, david.location_tonight)
$ bin/rails generate scaffold article title:string body:text
require 'test_helper' class ArticleTest < ActiveSupport::TestCase # test "the truth" do # assert true # end end
bin/rails test test/models/article_test.rb
bin/rails test test/models/article_test.rb test_the_truth
class Article < ActiveRecord::Base validates :title, presence: true end
class ArticleTest < ActiveSupport::TestCase test "the truth" do assert true end test "should not save article without title" do article = Article.new assert_not article.save, "Saved the article without a title" end end
test "should get new" do get new_article_url assert_response :success end
test "should show article" do get article_url(@article) assert_response :success end