ruby on rails - Rspec: Testing to make sure Notification Email is *not* sent after invalid record (ActiveRecord::RecordInvalid:) -


i'm trying test make sure notification mailer not sending after invalid record keep getting below error before test can complete

"activerecord::recordinvalid:

      'does not call send_email_notification'         expect(notificationmailer).not_to receive(:user_notification)         factorygirl.create(:invalid_user, shop: shop)       end 

how can test properly?

edit: here's code mail gets sent:

after_create :send_email_notification     private     def send_email_notification     if self.shop.email_notifications         notificationmailer.user_notification(self).deliver_now     end   end end 

it 'does not send notification email when user invalid'   expect(notificationmailer).not_to receive(:user_notification)   post :create, user: attributes_for(:invalid_user) end 

so, doing set you're expectation way did, , post user_controller create method invalid_user attributes.

of course, post shouldn't allowed create record if have set validations correctly in user model, , subsequently not call notificationmailer.user_notification.

note attributes_for factorygirl method can use arrange , pass factory attributes controller params.

now! why not work original approach? is because factorygirl complaining cannot create record, , absolutely logical since you're trying create invalid user. failing error has nothing testing email notification, rather way setup factory.

final note! if run test , complains:

"nomethoderror: undefined method `post' #<rspec::examplegroups" 

it means spec file not located under spec/controllers.

post, create, patch, delete methods part of rspec::rails::controllerexamplegroup

to solve please refer following stackoverflow answer

hope helps.


Comments