Rails 4 Action Mailer Previews and Factory Problems with Girls

I ran into a rather annoying problem when working with the Rails 4 mailing list preview and factory girl. Here is an example of my code:

class TransactionMailerPreview < ActionMailer::Preview def purchase_receipt account = FactoryGirl.build_stubbed(:account) user = account.owner transaction = FactoryGirl.build_stubbed(:transaction, account: account, user: user) TransactionMailer.purchase_receipt(transaction) end end 

It really can be viewing any email program. Suppose I am mistaken (something happens every time), and there is an error. I fix the error and refresh the page. Every time this happens, I get:

"ArgumentError in Rails :: MailersController # preview A copy of the User has been deleted from the module tree, but is still active!"

Then my only way out is to restart my server.

Am I missing something? Any clue as to what causes this and how can this be avoided? Because of this, I restarted my server 100 times in the last week.

EDIT: Can this happen anytime when I edit my code and update the preview?

+7
ruby-on-rails-4 factory-bot actionmailer
source share
1 answer

Although this is not quite the answer (but perhaps a hint), I also had this problem.

Are your factories making any records actually be kept?

I ended up using Factory.build where I could, and left everything else with private methods and OpenStructs to make sure all objects are fresh with every reboot, and nothing stops being rebooted.

I am wondering if the fact that FactoryGirl.build_stubbed tricking the system into thinking that objects are being saved makes the system try and restart them (after they leave).

Here is a snippet of what works for me:

 class SiteMailerPreview < ActionMailer::Preview def add_comment_to_page page = FactoryGirl.build :page, id: 30, site: cool_site user = FactoryGirl.build :user comment = FactoryGirl.build :comment, commentable: page, user: user SiteMailer.comment_added(comment) end private # this works across reloads where `Factory.build :site` would throw the error: # A copy of Site has been removed from the module tree but is still active! def cool_site site = FactoryGirl.build :site, name: 'Super cool site' def site.users user = OpenStruct.new(email: ' recipient@example.com ') def user.settings(sym) OpenStruct.new(comments: true) end [user] end site end end 

Although I am not completely satisfied with this approach, I no longer get these errors.

I would be interested to hear if anyone has a better solution.

0
source share

All Articles