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
$ 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