ryan42 (11) [Avatar] Offline
#1
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 (11) [Avatar] Offline
#2
Re: 12.1.4 Action mailer test problem with email?
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 (11) [Avatar] Offline
#3
Re: 12.1.4 Action mailer test problem with email?
fixed. had code mixed up and missing in comments.rb and tickets.rb
ryanbigg (423) [Avatar] Offline
#4
Re: 12.1.4 Action mailer test problem with email?
This wasn't the book's fault... was it?
ryan42 (11) [Avatar] Offline
#5
Re: 12.1.4 Action mailer test problem with email?
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 (11) [Avatar] Offline
#6
Re: 12.1.4 Action mailer test problem with email?
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 (423) [Avatar] Offline
#7
Re: 12.1.4 Action mailer test problem with email?
I think it should be doing self.watchers << user unless self.watchers.include?(user) at the bottom of page 317.

Good spot!
ryan42 (11) [Avatar] Offline
#8
Re: 12.1.4 Action mailer test problem with email?
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 (4) [Avatar] Offline
#9
Re: 12.1.4 Action mailer test problem with email?
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 (4) [Avatar] Offline
#10
Re: 12.1.4 Action mailer test problem with email?
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 (6) [Avatar] Offline
#11
Re: 12.1.4 Action mailer test problem with email?
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 (423) [Avatar] Offline
#12
Re: 12.1.4 Action mailer test problem with email?
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 (4) [Avatar] Offline
#13
Re: 12.1.4 Action mailer test problem with email?
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 (6) [Avatar] Offline
#14
Re: 12.1.4 Action mailer test problem with email?
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 (6) [Avatar] Offline
#15
Re: 12.1.4 Action mailer test problem with email?
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.
ryanbigg (423) [Avatar] Offline
#16
Re: 12.1.4 Action mailer test problem with email?
That's fantastic! Thanks for doing that research.

I will add that to the errata.