rspec / rspec-rails

RSpec for Rails 6+
https://rspec.info
MIT License
5.18k stars 1.04k forks source link

URL helpers in mailer specs? #94

Closed wincent closed 14 years ago

wincent commented 14 years ago

Updating an app from RSpec 1.3 to RSpec 2.0 I've noticed that the standard URL helpers aren't available in my mailer specs any more.

I can't remember doing anything special in RSpec 1.3 to make them available, and looking at the RSpec 1.3 code I can't see anything explicit in there to make the helpers available either, but I might be overlooking it.

In RSpec 2.0 I've had to add something like the following "include" to make the helpers available:

describe CommentMailer do
  # need to explicitly include the helpers or we get a NoMethodError
  # each time we try to use one
  include Rails.application.routes.url_helpers

  # need this too otherwise we'll get an exception, "Missing host to link to!
  # Please provide :host parameter or set default_url_options[:host]"
  Rails.configuration.action_mailer.default_url_options.each do |key, value|
    default_url_options[key] = value
  end

  it 'should include a link to the comment edit form' do
    @mail.body.should match(/#{edit_comment_url(@comment)}/)
  end
end

Do you think it would be a good idea to add the helpers in by default? As in, roll it into ./lib/rspec/rails/example/mailer_example_group.rb? Something like the following (not tested yet, just a demo of the basic idea):

diff --git a/lib/rspec/rails/example/mailer_example_group.rb b/lib/rspec/rails/example/mailer_example_group.rb
index 60c8c9b..c8ba3dd 100644
--- a/lib/rspec/rails/example/mailer_example_group.rb
+++ b/lib/rspec/rails/example/mailer_example_group.rb
@@ -11,6 +11,11 @@ module RSpec::Rails
     include RSpec::Matchers

     module ClassMethods
+      include Rails.application.routes.url_helpers
+      Rails.configuration.action_mailer.default_url_options.each do |key, value|
+        default_url_options[key] = value
+      end
+
       def mailer_class
         describes
       end

If you think it's a good idea let me know and I'll prepare a patch. In the meantime though I'll just stick something in my spec_helper so that I don't have to repeat the above set-up in all my mailer specs.

If it's a bad idea please be gentle as I am feeling a little sleep-deprived and possibly stupid right now.

(No idea whether I am going to run into similar issues with url_helpers in other types of specs... I only just started converting this app and have started with the mailer specs...)

Cheers, Wincent

wincent commented 14 years ago

For reference, this is my workaround that I've stuck in spec/support/mailer_spec_helpers.rb:

module MailerSpecHelpers
  extend ActiveSupport::Concern

  included do
    Rails.configuration.action_mailer.default_url_options.each do |key, value|
      default_url_options[key] = value
    end
  end

  include Rails.application.routes.url_helpers
end

Then in the mailer spec itself:

describe CommentMailer do
  include MailerSpecHelpers

  ...
end
dchelimsky commented 14 years ago

Seems like a good addition. Patch would be welcome. Get some sleep first, though.

wincent commented 14 years ago

Ok, have pushed a patch to the "issue-94" branch of my fork:

http://github.com/wincent/rspec-rails/commits/issue-94

Specifically, this commit:

http://github.com/wincent/rspec-rails/commit/fa34d8df79fb2d4fc6bde22437fa677a5076aa7b

Cheers, Wincent

nashbridges commented 2 years ago

Here's the merged commit for the reference.