Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8175311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:44:11+00:00 2026-06-06T22:44:11+00:00

I am trying to testing sending email in my rails 3 app, and am

  • 0

I am trying to testing sending email in my rails 3 app, and am testing with cucumber/rspec. In my test, I want the user to be able to receive an email, but instead of sending one, it is sending two. Here is my feature:

features/ticket_notications.feature

Feature: Ticket Notifications
Background:
Given there are the following users:
| email              | password |
| alice@ticketee.com | password |
| bob@ticketee.com   | password |

Given a clear email queue

Given there is a project called "TextMate 2"
And "alice@ticketee.com" can view the "TextMate 2" project
And "bob@ticketee.com" can view the "TextMate 2" project
And "alice@ticketee.com" has created a ticket for this project:
| title        | description       |
| Release date | TBA very shortly. |

Given I am signed in as "bob@ticketee.com"
Given I am on the homepage

Scenario: Ticket owner is automatically subscribed to a ticket
  When I follow "TextMate 2"
  And I follow "Release date"
  And I fill in "Text" with "Is it out yet?"
  And I press "Create Comment"
  Then "alice@ticketee.com" should receive an email
  When "alice@ticketee.com" opens the email
  Then they should see "updated the Release date ticket" in the email body
  And they should see "[ticketee] TextMate 2 - Release date" in the email subject
  Then they click the first link in the email
  Then I should see "Release date" within "#ticket h2"

When I run this I get this error:

Then "alice@ticketee.com" should receive an email                               
  # features/step_definitions/email_steps.rb:5
  expected: 1
  got: 2 (using ==)* (RSpec::Expectations::ExpectationNotMetError)
  ./features/step_definitions/email_steps.rb:52:in `/^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/'
  features\ticket_notifications.feature:26:in `Then "alice@ticketee.com" should receive an email'

So for some reason it’ receiving two emails instead of one, as seen in this line
expected: 1
got: 2

Here is some more code:

app/observers/comment_observer

class CommentObserver < ActiveRecord::Observer
  def after_create(comment)
    (comment.ticket.watchers - [comment.user]).each do |user|
   Notifier.comment_updated(comment, user).deliver
end
  end
end

app/models/ticket.rb

has_and_belongs_to_many :watchers, :join_table => "ticket_watchers",
:class_name => "User"

after_create :creator_watches_me

private
  def creator_watches_me
    self.watchers << user
  end

app/config/application.rb

 config.active_record.observers = :comment_observer

app/mailers/notifier.rb

class Notifier < ActionMailer::Base
default from: "ticketee@gmail.com"

def comment_updated(comment, user)
@comment = comment
@user = user
mail(:to => user.email, 
     :subject => "[ticketee] #{comment.ticket.project.name} - #{comment.ticket.title}")
  end
end

So, can anyone help me figure out why rspec reporting two emails instead of only one?

Edit: The code for the failing step:

features/step_definitions/email_steps.rb

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

Edit 2:
Here is the code from the log file

https://gist.github.com/2294406

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:44:17+00:00Added an answer on June 6, 2026 at 10:44 pm

    From the look of it, I think we have the same problem. I’m following the same book (Rails 3 in Action) and got stuck in the same place. I couldn’t detect where exactly the problem is, but when I put :

    class CommentObserver < ActiveRecord::Observer
      def after_create(comment)
        p comment.ticket.watchers
        (comment.ticket.watchers - [comment.user]).each do |user|
          Notifier.comment_updated(comment, user).deliver
        end
      end
    end
    

    I saw that the watchers array of comment object contains duplicate of the same user:

    [#<User id: 1, email: "alice@ticketee.com", encrypted_password: "$2a$04$D27BhCTxcOqsidMSDhp0FebonA82npQ01iFib3zwYCfT...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2012-07-02 15:32:42", updated_at: "2012-07-02 15:32:42", confirmation_token: nil, confirmed_at: "2012-07-02 15:32:42", confirmation_sent_at: "2012-07-02 15:32:42", admin: false>, #<User id: 1, email: "alice@ticketee.com", encrypted_password: "$2a$04$D27BhCTxcOqsidMSDhp0FebonA82npQ01iFib3zwYCfT...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2012-07-02 15:32:42", updated_at: "2012-07-02 15:32:42", confirmation_token: nil, confirmed_at: "2012-07-02 15:32:42", confirmation_sent_at: "2012-07-02 15:32:42", admin: false>]
    

    I dunno why, but I just created a workaround on this by putting an array of user id inside the sending loop :

    class CommentObserver < ActiveRecord::Observer
      def after_create(comment)
        sent = []
        (comment.ticket.watchers - [comment.user]).each do |user|
          Notifier.comment_updated(comment, user).deliver unless sent.include?(user.id)
          sent << user.id
        end
      end
    end
    

    Dirty, but it worked. I’m hoping someone can figure out what happened here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use OCMock for testing my app. But I am confused
I am trying to learn unit testing. I am trying to unit test some
I am trying to build a test program in c++ to automate testing for
I'm testing out sending email through my vps and I've run into a problem
I'm trying to mimic a pager with my app by sending push notifications to
Question Clarification: I'm trying to test if the user is authenticated or not for
I'm trying to send an email from a perl script but I'm having a
I am just testing and trying to learn how assembler works with C. So
I am testing JavaFX 2.1 and trying to get editable table views to behave
I am trying to do headless testing of my knockout viewmodels. I purposely avoid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.