方法
- A
实例公共方法
assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &block) 链接
断言在调用传递的代码块之前和之后,表达式求值的返回值发生了改变。
assert_changes 'Status.all_good?' do
post :create, params: { status: { ok: false } }
end
你可以将代码块作为字符串传递,以便在代码块的上下文中进行求值。也可以将 lambda 传递给代码块。
assert_changes -> { Status.all_good? } do
post :create, params: { status: { ok: false } }
end
这个断言对于测试副作用很有用。传递的代码块可以是任何可以转换为字符串的类型,可以使用 to_s 方法。
assert_changes :@object do
@object = 42
end
关键字参数 :from
和 :to
可以用来指定期望的初始值和代码块执行后的期望值。
assert_changes :@object, from: nil, to: :foo do
@object = :foo
end
可以指定一个错误信息。
assert_changes -> { Status.all_good? }, 'Expected the status to be bad' do
post :create, params: { status: { incident: true } }
end
# File activesupport/lib/active_support/testing/assertions.rb, line 195 def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &block) exp = expression.respond_to?(:call) ? expression : -> { eval(expression.to_s, block.binding) } before = exp.call retval = _assert_nothing_raised_or_warn("assert_changes", &block) unless from == UNTRACKED rich_message = -> do error = "Expected change from #{from.inspect}, got #{before.inspect}" error = "#{message}.\n#{error}" if message error end assert from === before, rich_message end after = exp.call rich_message = -> do code_string = expression.respond_to?(:call) ? _callable_to_source_string(expression) : expression error = "`#{code_string}` didn't change" error = "#{error}. It was already #{to.inspect}" if before == to error = "#{message}.\n#{error}" if message error end refute_equal before, after, rich_message unless to == UNTRACKED rich_message = -> do error = "Expected change to #{to.inspect}, got #{after.inspect}\n" error = "#{message}.\n#{error}" if message error end assert to === after, rich_message end retval end
assert_difference(expression, *args, &block) 链接
测试由于在传递的代码块中执行的操作导致的,表达式求值的返回值的数值差异。
assert_difference 'Article.count' do
post :create, params: { article: {...} }
end
传递一个任意表达式并求值。
assert_difference 'Article.last.comments(:reload).size' do
post :create, params: { comment: {...} }
end
可以指定任意正或负差异。默认值为 1
。
assert_difference 'Article.count', -1 do
post :delete, params: { id: ... }
end
也可以传递并求值一个表达式的数组。
assert_difference [ 'Article.count', 'Post.count' ], 2 do
post :create, params: { article: {...} }
end
也可以传递并求值一个表达式的数值差异的哈希。
assert_difference ->{ Article.count } => 1, ->{ Notification.count } => 2 do
post :create, params: { article: {...} }
end
可以传递并求值一个 lambda 或一个 lambda 的列表
assert_difference ->{ Article.count }, 2 do
post :create, params: { article: {...} }
end
assert_difference [->{ Article.count }, ->{ Post.count }], 2 do
post :create, params: { article: {...} }
end
可以指定一个错误信息。
assert_difference 'Article.count', -1, 'An Article should be destroyed' do
post :delete, params: { id: ... }
end
# File activesupport/lib/active_support/testing/assertions.rb, line 101 def assert_difference(expression, *args, &block) expressions = if expression.is_a?(Hash) message = args[0] expression else difference = args[0] || 1 message = args[1] Array(expression).index_with(difference) end exps = expressions.keys.map { |e| e.respond_to?(:call) ? e : lambda { eval(e, block.binding) } } before = exps.map(&:call) retval = _assert_nothing_raised_or_warn("assert_difference", &block) expressions.zip(exps, before) do |(code, diff), exp, before_value| actual = exp.call rich_message = -> do code_string = code.respond_to?(:call) ? _callable_to_source_string(code) : code error = "`#{code_string}` didn't change by #{diff}, but by #{actual - before_value}" error = "#{message}.\n#{error}" if message error end assert_equal(before_value + diff, actual, rich_message) end retval end
assert_no_changes(expression, message = nil, from: UNTRACKED, &block) 链接
断言在调用传递的代码块之前和之后,表达式求值的返回值没有发生改变。
assert_no_changes 'Status.all_good?' do
post :create, params: { status: { ok: true } }
end
提供可选的关键字参数 :from
来指定期望的初始值。
assert_no_changes -> { Status.all_good? }, from: true do
post :create, params: { status: { ok: true } }
end
可以指定一个错误信息。
assert_no_changes -> { Status.all_good? }, 'Expected the status to be good' do
post :create, params: { status: { ok: false } }
end
# File activesupport/lib/active_support/testing/assertions.rb, line 252 def assert_no_changes(expression, message = nil, from: UNTRACKED, &block) exp = expression.respond_to?(:call) ? expression : -> { eval(expression.to_s, block.binding) } before = exp.call retval = _assert_nothing_raised_or_warn("assert_no_changes", &block) unless from == UNTRACKED rich_message = -> do error = "Expected initial value of #{from.inspect}, got #{before.inspect}" error = "#{message}.\n#{error}" if message error end assert from === before, rich_message end after = exp.call rich_message = -> do code_string = expression.respond_to?(:call) ? _callable_to_source_string(expression) : expression error = "`#{code_string}` changed" error = "#{message}.\n#{error}" if message error end if before.nil? assert_nil after, rich_message else assert_equal before, after, rich_message end retval end
assert_no_difference(expression, message = nil, &block) 链接
断言在调用传递的代码块之前和之后,表达式求值的数值结果没有发生改变。
assert_no_difference 'Article.count' do
post :create, params: { article: invalid_attributes }
end
可以传递并求值一个 lambda。
assert_no_difference -> { Article.count } do
post :create, params: { article: invalid_attributes }
end
可以指定一个错误信息。
assert_no_difference 'Article.count', 'An Article should not be created' do
post :create, params: { article: invalid_attributes }
end
也可以传递并求值一个表达式的数组。
assert_no_difference [ 'Article.count', -> { Post.count } ] do
post :create, params: { article: invalid_attributes }
end
assert_not(object, message = nil) 链接
断言一个表达式不是真值。如果 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'
assert_nothing_raised() 链接
断言代码块不应该抛出异常。
如果传递的代码块中执行的代码没有抛出异常,则通过。
assert_nothing_raised do
perform_service(param: 'no_exception')
end
assert_raises(*exp, match: nil, &block) 链接
断言代码块抛出了 exp
中的一个异常。这是对标准 Minitest
断言方法的增强,它能够测试错误信息。
assert_raises(ArgumentError, match: /incorrect param/i) do
perform_service(param: 'exception')
end
也称为: assert_raise