{"id":6450,"date":"2014-04-18T05:38:34","date_gmt":"2014-04-18T05:38:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/18\/using-rspec-to-test-actionmailer-with-should_receive-collection-of-common-programming-errors-2\/"},"modified":"2014-04-18T05:38:34","modified_gmt":"2014-04-18T05:38:34","slug":"using-rspec-to-test-actionmailer-with-should_receive-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/18\/using-rspec-to-test-actionmailer-with-should_receive-collection-of-common-programming-errors-2\/","title":{"rendered":"Using rspec to test ActionMailer with should_receive-Collection of common programming errors"},"content":{"rendered":"<p>You&#8217;re likely calling <code>Mailer.notification_to_sender.deliver<\/code> in your controller, or better yet, a background job. I&#8217;m guessing notification_to_sender probably takes a parameter as well.<\/p>\n<p>Anyways, when you call the <code>notification_to_sender<\/code> method on Mailer you&#8217;re getting back an instance of <code>Mail::Message<\/code> that has the <code>deliver<\/code> method on it. If you were simply doing <code>Mailer.notification_to_sender<\/code> without also calling <code>deliver<\/code>, you could run what you have there with the comments and all would be fine. I would guess you&#8217;re also calling <code>deliver<\/code> though.<\/p>\n<p>In that case your failure message would be something like<\/p>\n<pre><code>NoMethodError:\n  undefined method `deliver' for nil:NilClass\n<\/code><\/pre>\n<p>That is because <code>nil<\/code> is Ruby&#8217;s default return value much of the time, which Rails also inherits. Without specifying the <code>mailer = mock<\/code> and <code>.and_return(mailer)<\/code> parts, when the controller executes in context of the test then notification_to_sender will return nil and the controller will try to call <code>deliver<\/code> on that nil object.<\/p>\n<p>The solution you have commented out is to mock out <code>notification_to_sender<\/code>&#8216;s return value (normally <code>Mail::Message<\/code>) and then expect that <code>deliver<\/code> method to be called on it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;re likely calling Mailer.notification_to_sender.deliver in your controller, or better yet, a background job. I&#8217;m guessing notification_to_sender probably takes a parameter as well. Anyways, when you call the notification_to_sender method on Mailer you&#8217;re getting back an instance of Mail::Message that has the deliver method on it. If you were simply doing Mailer.notification_to_sender without also calling deliver, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6450","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6450"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6450\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}