百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

capybara-email

> 编程语言
开源

用 Capybara 测试您的动作邮件和邮件信件

339 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用 Capybara 测试您的动作邮件和邮件信件

CapybaraEmail

Easily test ActionMailer and Mail messages in your Capybara integration tests

Installation

Add this line to your application's Gemfile:

gem 'capybara-email'

And then execute:

$ bundle

Or install it yourself as:

$ gem install capybara-email

Usage

RSpec

In your spec_helper.rb require capybara/email/rspec.

require 'capybara/email/rspec'

Example:

…

Cucumber

Require capybara/email in your features/support/env.rb

require 'capybara/email'

Once you have required capybara-email, gaining access to usable methods is easy as adding this module to your Cucumber World:

World(Capybara::Email::DSL)

I recommend adding this to a support file such as features/support/capybara_email.rb

require 'capybara/email'
World(Capybara::Email::DSL)

Example:

Scenario: Email is sent to winning user
  Given "[email protected]" is playing a game
  When that user picks a winning piece
  Then "[email protected]" receives an email with "You've Won!" as the subject

Then /^"([^"]*)" receives an email with "([^"]*)" as the subject$/ do |email_address, subject|
  open_email(email_address)
  expect(current_email.subject).to eq subject
end

Test::Unit

Require capybara/email at the top of test/test_helper.rb

  require 'capybara/email'

Include Capybara::Email::DSL in your test class

class ActionDispatch::IntegrationTest
  include Capybara::Email::DSL
end

Example:

…

CurrentEmail API

The current_email method will delegate all necessary method calls to Mail::Message. So if you need to access the subject of an email:

current_email.subject

Check out API for the mail gem for details on what methods are available.

Setting your test host

When testing, it's common to want to open an email and click through to your application. To do this, you'll probably need to update your test environment, as well as Capybara's configuration.

By default, Capybara's app_host is set to http://example.com. You should update this so that it points to the same host as your test environment. In our example, we'll update both to http://localhost:3001:

# tests/test_helper.rb
ActionDispatch::IntegrationTest do
  Capybara.server_port = 3001
  Capybara.app_host = 'http://localhost:3001'
end

# config/environments/test.rb
config.action_mailer.default_url_options = { host: 'localhost', 
                                             port: 3001 }

Sending Emails with JavaScript

Sending emails asynchronously will cause #open_email to not open the correct email or not find any email at all depending on the state of the email queue. We recommend forcing a sleep prior to trying to read any email after an asynchronous event:

click_link 'Send email'
sleep 0.1
open_email '[email protected]'

Authors

Brian Cardarella

We are very thankful for the many contributors

Versioning

This gem follows Semantic Versioning

Want to help?

Stable branches are created based upon each minor version. Please make pull requests to specific branches rather than master.

Please make sure you include tests!

Don't use tabs to indent, two spaces are the standard.

Legal

DockYard, Inc. © 2014

@dockyard

Licensed under the MIT license

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言