跳至内容 跳至搜索
方法
A
C

实例公共方法

assert_no_notifications(pattern = nil, &block)

断言在给定的 pattern 下没有发出通知。

你可以通过传递一个模式(接受字符串或正则表达式)和一个代码块来断言没有发出通知。在执行代码块时,如果没有发出匹配的通知,则断言通过。

assert_no_notifications("post.submitted") do
  post.destroy # => emits non-matching notification
end
# File activesupport/lib/active_support/testing/notification_assertions.rb, line 66
def assert_no_notifications(pattern = nil, &block)
  notifications = capture_notifications(pattern, &block)
  error_message = if pattern
    "Expected no notifications for #{pattern} but found #{notifications.size}"
  else
    "Expected no notifications but found #{notifications.size}"
  end
  assert_empty(notifications, error_message)
end

assert_notification(pattern, payload = nil, &block)

断言使用给定的 pattern 和可选的 payload 发出了通知。

你可以通过传递一个模式(接受字符串或正则表达式)、一个可选的 payload 和一个代码块来断言发出了通知。在执行代码块时,如果发出了匹配的通知,则断言通过,并返回该通知。

请注意,payload 是作为子集匹配的,这意味着通知必须至少包含指定的键和值,但可能包含额外的键值对。

assert_notification("post.submitted", title: "Cool Post") do
  post.submit(title: "Cool Post", body: "Cool Body") # => emits matching notification
end

使用返回的通知,你可以进行更自定义的断言。

notification = assert_notification("post.submitted", title: "Cool Post") do
  ActiveSupport::Notifications.instrument("post.submitted", title: "Cool Post", body: Body.new("Cool Body"))
end

assert_instance_of(Body, notification.payload[:body])
# File activesupport/lib/active_support/testing/notification_assertions.rb, line 28
def assert_notification(pattern, payload = nil, &block)
  notifications = capture_notifications(pattern, &block)
  assert_not_empty(notifications, "No #{pattern} notifications were found")

  return notifications.first if payload.nil?

  notification = notifications.find { |notification| notification.payload.slice(*payload.keys) == payload }
  assert_not_nil(notification, "No #{pattern} notification with payload #{payload} was found")

  notification
end

assert_notifications_count(pattern, count, &block)

断言具有给定 pattern 的通知发出的数量。

你可以通过传递一个模式(接受字符串或正则表达式)、一个计数和一个代码块来断言发出的通知数量。在执行代码块时,将计算匹配通知的数量。在代码块执行完成后,如果计数匹配,则断言通过。

assert_notifications_count("post.submitted", 1) do
  post.submit(title: "Cool Post") # => emits matching notification
end
# File activesupport/lib/active_support/testing/notification_assertions.rb, line 51
def assert_notifications_count(pattern, count, &block)
  actual_count = capture_notifications(pattern, &block).count
  assert_equal(count, actual_count, "Expected #{count} instead of #{actual_count} notifications for #{pattern}")
end

capture_notifications(pattern = nil, &block)

捕获发出的通知,可选择按 pattern 过滤。

你可以捕获发出的通知,可选择通过一个模式(接受字符串或正则表达式)和一个代码块来过滤。

notifications = capture_notifications("post.submitted") do
  post.submit(title: "Cool Post") # => emits matching notification
end
# File activesupport/lib/active_support/testing/notification_assertions.rb, line 85
def capture_notifications(pattern = nil, &block)
  notifications = []
  ActiveSupport::Notifications.subscribed(->(n) { notifications << n }, pattern, &block)
  notifications
end