> For the complete documentation index, see [llms.txt](https://kelby.gitbook.io/rails-beginner-s-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kelby.gitbook.io/rails-beginner-s-guide/testing/testcase_actionmailer.md).

# Action Mailer

## Test Helper

默认 Rails 提供两个 helper 方法用于测试：

| 方法                           | 解释                                 |
| ---------------------------- | ---------------------------------- |
| assert\_emails               | 断言已经发送的邮件数                         |
| assert\_no\_emails           | 断言没有邮件发送出去(可用 assert\_emails 0 代替) |
| assert\_enqueued\_emails     | 断言邮件已进队列                           |
| assert\_no\_enqueued\_emails | 断言邮件不在队列里                          |

assert\_emails 和 assert\_no\_emails 两者本质都是封装 assert\_equal.

## ~~Behavior~~

类方法：

```
determine_default_mailer

mailer_class

tests
```

~~实例方法：~~

```
initialize_test_deliveries

restore_delivery_method
restore_test_deliveries

set_delivery_method
set_expected_mail
```

除通常的测试方法外，还有 `deliveries` 方法获取已经发送的邮件实例。

使用举例:

```ruby
last_email = ActionMailer::Base.deliveries.last

expect(last_email.to).to eq ['test@example.com']
expect(last_email.subject).to have_content 'Welcome'

email = UserMailer.confirmation(user.id).deliver_now

assert ActionMailer::Base.deliveries.any?
assert_equal [user.email], email.to
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kelby.gitbook.io/rails-beginner-s-guide/testing/testcase_actionmailer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
