EzDevInfo.com

email-spec

Collection of RSpec/MiniTest matchers and Cucumber steps for testing email in a ruby app using ActionMailer or Pony

How to test - with rspec - what template is used when creating an email? (Rails 3.0.7)

I'm trying to write some tests for emails generated with a mailer class using rspec and email_spec (Ruby on Rails 3.0.7)

I would like to check if the layout used for rendering the email is the layout that was specified in the mailer class.

Any idea on how to do this? I've spent 3 hours searching for a solution but couldn't find anything.

Thanks!


Source: (StackOverflow)

Cucumber/Capybara/Email Spec not working correctly with Selenium Driver

I've running some Cucumber/Capybara tests. I've been using the email_spec gem to check email stuff. Some steps where of the kind 'And "someone@email.com" should receive an email'. They give no problem when I run the test using the rack_test driver. However, they fail when using the selenium driver:

And "someone@email.com" should receive an email                                                                
  expected: 1
       got: 0 (using ==) (RSpec::Expectations::ExpectationNotMetError)

Can you help me? Thanks


Source: (StackOverflow)

Advertisements

How do I test Pony emailing in a Sinatra app, using rspec?

I'm trying out email_spec, which says it supports Pony, but I'm not sure how I'd go about testing emails in a sinatra app. The examples in the readme show usages with rails ActionMailer, but not in Pony.

Not precious about using email_spec, so any other ideas for testing emails using rspec in sinatra are welcomed =)


Source: (StackOverflow)

How to resolve a gem dependency conflict after installing email_spec

I just installed email-spec for use with rspec according to the instructions in the readme. Now, when I try to run a test (even without using email-spec's methods in my specs) I get this error:

$ rspec spec
Neither Pony nor ActionMailer appear to be loaded so email-spec is requiring ActionMailer.
WARN: Unresolved specs during Gem::Specification.reset:
      launchy (~> 2.1)
      builder (~> 3.1)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
/Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.7.4/lib/bundler/runtime.rb:34:in `block in setup': You have already activated minitest 5.4.0, but your Gemfile requires minitest 4.7.5. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.7.4/lib/bundler/runtime.rb:19:in `setup'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.7.4/lib/bundler.rb:121:in `setup'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.7.4/lib/bundler/setup.rb:7:in `<top (required)>'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:128:in `require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:128:in `rescue in require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:39:in `require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
    from /Users/jpalmieri/workspace/blocitoff/config/boot.rb:4:in `<top (required)>'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:121:in `require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:121:in `require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
    from /Users/jpalmieri/workspace/blocitoff/config/application.rb:1:in `<top (required)>'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:121:in `require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:121:in `require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
    from /Users/jpalmieri/workspace/blocitoff/config/environment.rb:2:in `<top (required)>'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:121:in `require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:121:in `require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
    from /Users/jpalmieri/workspace/blocitoff/spec/rails_helper.rb:4:in `<top (required)>'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:121:in `require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:121:in `require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
    from /Users/jpalmieri/workspace/blocitoff/spec/features/mark_todo_complete_spec.rb:1:in `<top (required)>'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:241:in `load'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:241:in `block in load'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:241:in `load'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/bin/rspec:23:in `load'
    from /Users/jpalmieri/.rbenv/versions/2.0.0-p353/bin/rspec:23:in `<main>'

Prepending bundle exec will make the tests run fine (I still get the Neither Pony nor ActionMailer appear to be loaded so email-spec is requiring ActionMailer notice, but I guess that's a topic for a different post).

Here's my Gemfile. I don't explicitly call minitest, so I'm assuming that it's a dependency of email_spec and something else.

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.10'

gem 'devise'
gem 'figaro', '1.0'
gem 'bootstrap-sass', '~> 3.3.1'
gem 'timecop'
gem 'pundit'

group :development, :test do
  gem 'rspec-rails'
  gem 'capybara'
end

# Use sqlite3 as the database for Active Record
group :production do
  gem 'pg'
  gem 'rails_12factor'
end

group :development do
  gem 'sqlite3'
end

group :test do
  gem 'factory_girl_rails', '~> 4.0'
  gem 'database_cleaner'
  gem 'email_spec'
end

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.2'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

I tried resolving the conflict by adding gem 'minitest', '~> 4.7.5' to my :test group, but still get the same error.

group :test do
  gem 'factory_girl_rails', '~> 4.0'
  gem 'database_cleaner'
  gem 'email_spec'
  gem 'minitest', '~> 4.7.5'
end

My questions are:

1) I've never had to prepend bundle exec before. Should I try to resolve the conflict instead of prepending bundle exec?

2) If "yes" to question 1, how would I go about doing this?


Source: (StackOverflow)

rspec-email - How to get the body text?

I'm using rspec with the email-spec gem. I'm trying to do:

last_delivery = ActionMailer::Base.deliveries.last
last_delivery.body.should include "This is the text of the email"

But that doesn't work, is there a way to say body, text version? Content-Type: text/text?

Thanks


Source: (StackOverflow)

How to write an RSpec for a Mail Interceptor?

I'm using a mail interceptor as follows:

setup_mail.rb

Mail.register_interceptor(MailInterceptor) if Rails.env != "production" 

class MailInterceptor

class MailInterceptor  
  def self.delivering_email(message)  
    message.subject = "#{message.subject} [#{message.to}]"
    message.to = "xxxxx@xxxxxx.com"
  end
end

I'm unable to create an rspec for this interceptor as it is not happen with rake spec.

I have the following spec:

  describe "MailInterceptor" do 
    it "should be intercepted" do
      @email = UserMailer.registration_automatically_generated(@user)
      @email.should deliver_to("xxxxx@xxxxxx.com")      
    end
  end

In the test.log I see that the deliver_to is not the interceptor. Any ideas on how I can write an rspec for the interceptor?

Thanks


Source: (StackOverflow)

Email spec doesn't match body content in Rails

I'm using email_spec gem to test a simple email, but for some reason the body content appears to be empty:

  1) ContactMailer welcome email to new user renders the body
     Failure/Error: mail.should have_body_text("Hi")
       expected the body to contain "Hi" but was ""
     # ./spec/mailers/contact_mailer_spec.rb:17:in `block (3 levels) in <top (required)>'

Every other example passes. The template file is called welcome_email.text.erb. Not sure why body is not matched, but the email does have a body when it gets sent.

Edit: the Rspec code is:

let(:mail) { ContactMailer.welcome_email(email) }


it "renders the body" do
  mail.should have_body_text("Hi")
end

Source: (StackOverflow)

integration tests for devise password recovery using rspec, capybara and email_spec

I'm trying to test the password recovery using rspec/capybara using email_spec

My test.rb contains:

config.action_mailer.delivery_method = :test

My test goes like this:

feature User do
  let!(:user){ FactoryGirl.build(:user) }

  before(:each) do
    visit root_path
    click_link "Sign up/in"
  end

  scenario "recover password" do
    user.save!
    click_link "Forgot password?"
    fill_in "Email", :with => user.email
    click_button "Send me reset password instructions"
    unread_emails_for(user.email).should be_present
  end

My test fails like so:

  1) User recover password
     Failure/Error: click_button "Send me reset password instructions"
     ActionView::Template::Error:
       Missing host to link to! Please provide the :host parameter,
       set default_url_options[:host], or set :only_path to true

If I set the default_url_options, rspec tries to send the mail. I'm not too sure about :only_path.

Can anyone please point me in the right direction? Many thanks.


Source: (StackOverflow)

Capybara+rspec:Reset_Password routing error

So I am trying to test the simple feature of forgot password.I am newbie, so excuse me if its a silly question. Here, I have to enter user email in forgot_password form which sends me a link in an email to reset password. I am reaching inside the email, but when it clicks the link,page ends up on error page

enter image description here

 it "sends reset password email on submit",:js=> true, :focus=>true do


    visit "/login"
    click_link "Forgot Password?"
    sleep 1
    fill_in "email", :with => @user.email
    click_button "Submit"
    email = open_email(@user.email)
    email.should deliver_to(@user.email)
    #email.should have_subject("FlinkMusic - Forgot you password")
    click_first_link_in_email

    page.should have_content ("Reset your password")
        fill_in "Password ", :with => "password1" 
        fill_in "Repeat Password", :with => "password1"
        click_button "Reset Password"
        page.should have_content "Your password has been reset.Please log in."

end

here is my spec_helper

   require 'rubygems'
   require 'spork'
   Spork.prefork do

   ENV["RAILS_ENV"] ||= 'test'
   require File.expand_path("../../config/environment", __FILE__)
   require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rspec'
  require "capybara-screenshot"
  require 'factory_girl_rails'
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

 #Capybara.defualt_wait_time = 10

  if ENV['BROWSER_TEST']
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
 end
else
Capybara.javascript_driver = :selenium
 end
RSpec.configure do |config|

      config.include Capybara::DSL

     config.include(EmailSpec::Helpers)
     config.include(EmailSpec::Matchers)


     config.treat_symbols_as_metadata_keys_with_true_values = true
     config.filter_run :focus => true
     config.run_all_when_everything_filtered = true

Is there anything I am missing it needs? Thanks!


Source: (StackOverflow)

Where must bu an email password?

What must I change in email_steps.rb except example@example.com in line 32?

How can this look at my email without a password?

I don't understan, must I change hier (in email_steps.rb) something else and use it like my test (whrite my test hier)?

Or must I write a new test, that will be use the commands from email_steps.rb???


Source: (StackOverflow)

How to do test integration of features based on delayed jobs with cucumber?

I want to test some features that needs delayed jobs to work, on cucumber. I have defined the following step:

Given /^jobs are being dispatched$/ do 
    Delayed::Worker.new.work_off
end

Right now, I am trying to test email notifications. So I have the following scenario:

Scenario: Receiving email when signing up   
      Given I am on the signup page
      And I fill in "user[email]" with "test@test.com" 
      And I fill in "user[password]" with "password"
      And I fill in "user[password_confirmation]" with "password"
      And I press "Sign up"
      Then I should be on the homepage
      Given jobs are being dispatched
      Then "test@test.com" should receive 1 emails

The should receive n emails step is defined by email_spec and is defined as:

Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/ do |address, amount|
  unread_emails_for(address).size.should == parse_email_count(amount)
end

So the test fails telling me that I am receiving 0 emails (the test@test.com is replaced by a real email in this test and I am not receiving anything). I suspect that the worker didn't really start. What should I check? By the way, when I test in development mode I really receive that email.

Thanks

Edit:

It looks like I am getting a SQLite3::BusyException:

SQLite3::BusyException: database is locked: INSERT INTO "delayed_jobs" ....

Now to investigate why and how I can get rid of that! Any idea? (besides moving my database to PostgreSQL or mySQL).

Edit: Ok, I moved to PostgreSQL from SQLite, the records are being inserted into Delayed::Job but the email tests fail.

The config/environments/test.rb file contains:

 config.action_mailer.delivery_method = :test
  config.action_mailer.perform_deliveries = true

  config.action_mailer.smtp_settings = {
     :address              => "smtp.gmail.com",
     :port                 => 587,
     :domain               => "mydomain.com",
     :user_name            => "name@mydomain.com",
     :password             => "mypassword",
     :authentication       => "plain",
     :enable_starttls_auto => true
   }

  config.action_mailer.default_url_options = { :host => 'localhost:3000' } 

Source: (StackOverflow)

rails rspec email_spec fails but real emailing works

Real emailing works but the test fails. Not sure whats going wrong. Does anyone know what could be the problem?

TasksController POST create with valid params emails the collaborators (FAILED - 1)

  it 'emails the collaborators' do
    post :create, { :task => valid_attributes }

    Emailer.should_receive(:send_email).with(user, :new_task, assigns(:task).project)
  end

Failures:

1) TasksController POST create with valid params emails the collaborators Failure/Error: Emailer.should_receive(:send_email).with(user, :new_task, assigns(:task).project) ().send_email(#, :new_task, 'blah')
expected: 1 time
received: 0 times

I have the following gems:
rails (3.2.7)
rspec-rails (2.11.4)
email_spec (1.2.1)


Source: (StackOverflow)

Email delivery testing with email_spec breaks on Rails 3.0.11 -> 3.1.3

I'm in process of migrating a rails 3.0.11 app to 3.1.3. And somehow, this breaks email_spec testing for amount of emails received when using cucumber + selenium setup.

config.action_mailer.delivery_method = :test, also have tried it with :cache and :file delivery methods. No matter what, ActionMailer::Base.deliveries is empty, even tho according to logs, the emails get sent. And dropping back down to to rails 3.0.11 makes everything work again.

Edit: For specific versions, here is my Gemfile.lock (note that spork is not used for cucumber features).

Clues, anyone?


Source: (StackOverflow)

Rails test env: why does it need default_url_options for ActionMailer?

Why does the test environment need default_url_options be set for ActionMailer?

If I don't set it, I'm getting this when executing my specs:

 ActionView::Template::Error:
   Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

I'm especially wondering because when setting

config.action_mailer.default_url_options = {host: 'localhost:3000'}

then in theory links in emails will lead to localhost:3000, which as far as I know is not where the test server instance really is running? Still, when using email_spec gem and clicking on links in emails, they work, because the gem removes the server name and port, so a link typically looks like this:

/en/user/confirmation?confirmation_token=QZu3tw17uozhpEfuVWzF

So one more time: what do I need to specify the host for if it's removed by email_spec gem anyway?


Source: (StackOverflow)

How to pass long URL in plain text emails?

I have this account creation email that is sent out to anyone who is trying to create an account as I need to authenticate that they are who they say they are.

However, my issue here is that the URL where they need to click when they receive my email is too long and some email clients do not handle that very well and sometimes truncates the URL thus making the URL invalid when clicked.

Because the URL contains the domain name, the hashed email and a long activation code. It looks something like this.

http://domain.com/activation?email=75a5867d3df134bededbaf24ff17624d&key=8fecb20817b3847419bb3de39a609afe

While some email clients are ok with this but some are not...And I don't want to use HTML email and rather stick with plain/text email. Also I heard horrible stories using URL shorteners so I am not sure if I should use them...

Any insights in this area is appreciated!


Source: (StackOverflow)