Integration test の Failed assertion, no message given への対応; Rails

Ruby で Integration テストしていた(ruby -Itest test/model/*.rb)

assert のテスト失敗時、常にFailed assertion, no message given.

と表示された。一概には言えないが、一つの、原因と対策

一つの原因

assert の第二引数に「エラーメッセージ」が渡されていない。

一つの対策

assert 評価式, 'error message'

で表記する。

ex

テストケース

「Book モデル(title:string > validates with presence) が、

new されただけで save されたときは false を返している」

Prepare

$ rails g model Book title:string

app/models/book.r

class Book < ActiveRecord::Base
  validates :title, presence: true
end

test/models/book.rb

# coding: utf-8
  
require 'test_helper'

class TestBook < ActiveSupport::TestCase
  test 'should not save' do
    book = Book.new
    assert !book.save
  end
end

この時点

$ ruby -Itest test/models/book.rb

しても

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

もし assert book.save にすると

$ ruby -Itest test/models/book.rb したときに

  1) Failure:
  TestBook#test_should_not_save [test/models/book.rb:8]:
=>  Failed assertion, no message given.

が出る。

こいつを消すためには

assert book.save, 'attributes are invalid!'

として、エラーメッセージを第二引数に渡さなければいけない


参考元 # thanks

Rails 3 in Action - Test-Driven Development - CodeProject