(Rails3.2.11)
動作確認用プロジェクト作成
$ rails new test_email_in_rspec -T --skip-bundle $ cd test_email_in_rspec
Gemfile
group :test, :development do gem 'rspec-rails' end
rspec:install
$ rails g rspec:install
動作確認用スキャフォールド・メーラー作成
$ rails g scaffold User name email $ rails g mailer user_mailer notice
config/environments/development.rb
いちおう設定
config.action_mailer.default_url_options = { :host => 'localhost:3000' } config.action_mailer.delivery_method = :test
spec/support/mailer_macros.rb
module MailerMacros def last_email ActionMailer::Base.deliveries.last end def reset_email ActionMailer::Base.deliveries = [] end end
/spec/spec_helper.rb
RSpec.configure do |config| #... config.include(MailerMacros) config.before(:each) { reset_email } end
spec/controllers/users_controller_spec.rb
describe "GET index" do it "assigns all users as @users" do user = User.create! valid_attributes get :index, {}, valid_session assigns(:users).should eq([user]) # add last_email.to.should == ["to@example.org"] end end
実行してみる
$ rake spec SPEC=spec/controllers/users_controller_spec.rb … Finished in 0.24565 seconds 16 examples, 0 failures