Deal of the Day

Home » Main » Manning Forums » 2009 » Rails 3 in Action

Thread: 12.1.4 Action mailer test problem with email?

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 15 - Pages: 2 [ 1 2 | Next ] - Last Post: Jan 23, 2012 12:11 AM by: ryanbigg Threads: [ Previous | Next ]
ryan42

Posts: 11
From: United States
Registered: 12/12/11
12.1.4 Action mailer test problem with email?
Posted: Dec 12, 2011 8:20 AM
  Click to reply to this thread Reply

Hi, I am having trouble following through the section in chapter 12 where the cucumber test fails on this step:
Then "alice@ticketee.com" should receive an email

This is the error I am getting, I have looked around with google and not been able to resolve it yet

Then "alice@ticketee.com" should receive an email
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:25:in `Then "alice@ticketee.com" should receive an email'

It seems that either I have an error in the email_steps file or somewhere else with email settings just not working right. Link to my code is below, if anyone has an ideas please let me know!

https://github.com/mrchameleon/ticketee

Thanks in advance.

ryan42

Posts: 11
From: United States
Registered: 12/12/11
Re: 12.1.4 Action mailer test problem with email?
Posted: Dec 12, 2011 9:22 AM   in response to: ryan42 in response to: ryan42
  Click to reply to this thread Reply

Ok nevermind, fixed. I figured out what I did. I looked at the book's final code on github and found that I had stuff missing and mixed up between the comment.rb and ticket.rb for this section. It is now seeing the email in my test!

ryan42

Posts: 11
From: United States
Registered: 12/12/11
Re: 12.1.4 Action mailer test problem with email?
Posted: Dec 12, 2011 9:23 AM   in response to: ryan42 in response to: ryan42
  Click to reply to this thread Reply

fixed. had code mixed up and missing in comments.rb and tickets.rb

ryanbigg

Posts: 407
From: Sydney
Registered: 6/18/10
Re: 12.1.4 Action mailer test problem with email?
Posted: Dec 12, 2011 8:14 PM   in response to: ryan42 in response to: ryan42
  Click to reply to this thread Reply

This wasn't the book's fault... was it?

ryan42

Posts: 11
From: United States
Registered: 12/12/11
Re: 12.1.4 Action mailer test problem with email?
Posted: Dec 13, 2011 9:00 AM   in response to: ryanbigg in response to: ryanbigg
  Click to reply to this thread Reply

Maybe so.. around 12.2.2, I think there may be missing info on what to put in the tickets.rb file. I cannot find in the book anywhere where it says to put the following in the tickets model: (these are the lines I found on github)


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

ryan42

Posts: 11
From: United States
Registered: 12/12/11
Re: 12.1.4 Action mailer test problem with email?
Posted: Dec 13, 2011 9:23 AM   in response to: ryan42 in response to: ryan42
  Click to reply to this thread Reply

I still may have something misplaced or wrong on that functionality. It's now down to a minor issue though: When I add a comment to a ticket, admin@ticketee.com starts watching automatically. Then if I add another comment, it shows admin@ticketee.com a second time under the watchers list, and so on. I can find and fix that later though. I'm on the API chapter now, If I get really stuck I will post a new thread here! Thanks for your help!

ryanbigg

Posts: 407
From: Sydney
Registered: 6/18/10
Re: 12.1.4 Action mailer test problem with email?
Posted: Dec 17, 2011 10:14 PM   in response to: ryan42 in response to: ryan42
  Click to reply to this thread Reply

I think it should be doing self.watchers << user unless self.watchers.include?(user) at the bottom of page 317.

Good spot!

ryan42

Posts: 11
From: United States
Registered: 12/12/11
Re: 12.1.4 Action mailer test problem with email?
Posted: Dec 29, 2011 9:26 AM   in response to: ryanbigg in response to: ryanbigg
  Click to reply to this thread Reply

Thanks! That's pretty obvious that the problem was occurring there now that you pointed it out. I also added your snippet to the comments model, referencing ticket instead of self, to ensure that the user wasn't already included in the watchers for the ticket when adding a comment and getting auto-added to watchers.


def creator_watches_ticket
ticket.watchers << user unless ticket.watchers.include?(user)
end

sedward

Posts: 4
From: Singapore
Registered: 1/15/12
Re: 12.1.4 Action mailer test problem with email?
Posted: Jan 15, 2012 4:38 AM   in response to: ryan42 in response to: ryan42
  Click to reply to this thread Reply

I had the same fail message:

Then "alice@ticketee.com" should receive an email
expected: 1
got: 2

Spent a good amount of time debugging, I found out there's 2 users inside self.watchers before this step occurs, hence failed.

I cross checked with the code countless of times, until I finally fixed it

turns out that if
after_create :creator_watches_me
is placed at the top of the ticket,rb class, right after the searcher lambda do-end block, it FAILS

when it's moved to the middle of the class, before the def tag! method, it PASSES!

Now, I'm pretty confused of what is going on here.. why does the placement of the after_create callback affects the outcome of the code?

Would appreciate it if someone can shed some light.

sedward

Posts: 4
From: Singapore
Registered: 1/15/12
Re: 12.1.4 Action mailer test problem with email?
Posted: Jan 15, 2012 4:40 AM   in response to: sedward in response to: sedward
  Click to reply to this thread Reply

This is my code

Note the placement of after_create callback



class Ticket < ActiveRecord::Base
searcher do
label :tag, :from => :tags, :field => :name
label :state, :from => :state, :field => :name
end

after_create :creator_watches_me <-- if placed here, failed!

belongs_to :project
belongs_to :user
belongs_to :state

validates :title, :presence => true
validates :description, :presence => true,
:length => { :minimum => 10 }
has_many :assets
accepts_nested_attributes_for :assets

has_many :comments

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

after_create :creator_watches_me <-- if placed here, passes!

def tag!(tags)
if tags
tags = tags.split(" ").map do |tag|
Tag.find_or_create_by_name(tag)
end

self.tags << tags
end
end

private
def creator_watches_me
self.watchers << user unless self.watchers.include?(user)
end
end

Message was edited by:
sedward

theirongiant

Posts: 6
Registered: 12/21/11
Re: 12.1.4 Action mailer test problem with email?
Posted: Jan 16, 2012 9:50 AM   in response to: sedward in response to: sedward
  Click to reply to this thread Reply

I put a logging lines before and after the self.watchers << self.user line and my output was

User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'alice@ticketee.com' LIMIT 1
SQL (0.2ms) INSERT INTO `tickets` (`created_at`, `description`, `project_id`, `state_id`, `title`, `updated_at`, `user_id`) VALUES ('2012-01-16 14:42:57', 'TBA very shortly', 25, NULL, 'Release date', '2012-01-16 14:42:57', 49)

[ Alice has created ticket ]

>>>> Creator_watches_me:alice@ticketee.com << [ Before self.watchers << self.user ]

(0.1ms) INSERT INTO `ticket_watchers` (`ticket_id`, `user_id`) VALUES (25, 49)
User Load (0.5ms) SELECT `users`.* FROM `users` INNER JOIN `ticket_watchers` ON `users`.`id` = `ticket_watchers`.`user_id` WHERE `ticket_watchers`.`ticket_id` = 25

>>>> Watcher count = 1 << [ After self.watchers << self.user ]

(0.1ms) INSERT INTO `ticket_watchers` (`ticket_id`, `user_id`) VALUES (25, 49) << [ ??? ]

-----------------------------------------------------

I've changed creator_watches_me to be called before_create and it seems to work as expected.

Message was edited by:
theirongiant

ryanbigg

Posts: 407
From: Sydney
Registered: 6/18/10
Re: 12.1.4 Action mailer test problem with email?
Posted: Jan 18, 2012 5:21 AM   in response to: sedward in response to: sedward
  Click to reply to this thread Reply

That's a rather interesting fail then. I wonder why the *ordering* matters. There's nothing else there other than validations and associations which should not bother it given that it is an after_create callback.

sedward

Posts: 4
From: Singapore
Registered: 1/15/12
Re: 12.1.4 Action mailer test problem with email?
Posted: Jan 18, 2012 8:25 AM   in response to: ryanbigg in response to: ryanbigg
  Click to reply to this thread Reply

thanks for replying!

your book is really great!! I'm on the API chapter now.

I gave up figuring this duplicate watchers issue, it's the only problem I encountered in this book thus far.

To all the guys who are stuck at this issue, temporary workaround is to move your after_create to the bottom of the ticket.rb, or as theirongiant says, change the after_create to a before_create callback.

theirongiant

Posts: 6
Registered: 12/21/11
Re: 12.1.4 Action mailer test problem with email?
Posted: Jan 18, 2012 8:32 AM   in response to: sedward in response to: sedward
  Click to reply to this thread Reply

i've been stepping through it with the debugger, the issue as far as i can see is that '<<' instantly triggers the first insert. From what i've managed to figure out at some point after that Ticket is trying to save it's associated objects (i'd have thought the after_create callback would have been called after that). I'm going to have a dig at it this evening but either the callback is occuring to soon or the watcher isn't being marked as persisted when it's initially saved.

theirongiant

Posts: 6
Registered: 12/21/11
Re: 12.1.4 Action mailer test problem with email?
Posted: Jan 18, 2012 9:24 AM   in response to: theirongiant in response to: theirongiant
  Click to reply to this thread Reply

https://github.com/rails/rails/issues/3639

looks like after_create should be added after the habtm association.

+ # == Callbacks
+ #
+ # Association with autosave option defines several callbacks on your
+ # model (before_save, after_create, after_update). Please note that
+ # callbacks are executed in the order they were defined in
+ # model. You should avoid modyfing the association content, before
+ # autosave callbacks are executed. Placing your callbacks after
+ # associations is usually a good practice.

Legend
Gold: 300 + pts
Silver: 100 - 299 pts
Bronze: 25 - 99 pts
Manning Author
Manning Staff
Manning Developmental Editor