модуль ActiveSupport::Testing::Assertions
Открытые методы экземпляров
# File activesupport/lib/active_support/testing/assertions.rb, line 63
def assert_difference(expression, difference = 1, message = nil, &block)
expressions = Array(expression)
exps = expressions.map { |e|
e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
}
before = exps.map { |e| e.call }
yield
expressions.zip(exps).each_with_index do |(code, e), i|
error = "#{code.inspect} didn't change by #{difference}"
error = "#{message}.\n#{error}" if message
assert_equal(before[i] + difference, e.call, error)
end
end Проверка числового различия между возвращаемым значением выражения в результате выполнения блока кода.
assert_difference 'Article.count' do
post :create, article: {...}
end Передаётся и вычисляется произвольное выражение.
assert_difference 'assigns(:article).comments(:reload).size' do
post :create, comment: {...}
end Можно указать любое положительное или отрицательное различие. По умолчанию это 1.
assert_difference 'Article.count', -1 do post :delete, id: ... end
Можно также передать и вычислить массив выражений.
assert_difference [ 'Article.count', 'Post.count' ], 2 do
post :create, article: {...}
end Можно передать и вычислить лямбда-выражение или список лямбда-выражений:
assert_difference ->{ Article.count }, 2 do
post :create, article: {...}
end
assert_difference [->{ Article.count }, ->{ Post.count }], 2 do
post :create, article: {...}
end Можно указать сообщение об ошибке.
assert_difference 'Article.count', -1, 'An Article should be destroyed' do post :delete, id: ... end
# File activesupport/lib/active_support/testing/assertions.rb, line 92 def assert_no_difference(expression, message = nil, &block) assert_difference expression, 0, message, &block end
Проверка, что числовое значение, полученное при вычислении выражения, не изменяется до и после вызова переданного блока.
assert_no_difference 'Article.count' do post :create, article: invalid_attributes end
Можно указать сообщение об ошибке.
assert_no_difference 'Article.count', 'An Article should not be created' do post :create, article: invalid_attributes end
# File activesupport/lib/active_support/testing/assertions.rb, line 17
def assert_not(object, message = nil)
message ||= "Expected #{mu_pp(object)} to be nil or false"
assert !object, message
end Проверка, что выражение не является истинным. Проходит, если object является nil или false. «Истинное» значение — это значение, считающееся истинным в условном операторе, например, if foo.
assert_not nil # => true assert_not false # => true assert_not 'foo' # => Expected "foo" to be nil or false
Можно указать сообщение об ошибке.
assert_not foo, 'foo should be false'
© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.