How to enable multiple modules in RSpec?

I'm not sure how to enable multiple modules in RSpec, so let me describe my situation.

In app/helpersI have two files with helpers containing modules ApplicationHelperand MailersHelper. Although these are view assistants that I use in my presentations and letters, I also use some of my methods in my tests, so they should be available in the sentence describe.

In app/spec/mailersI also have a file containing the module Helpers. This module contains methods that should only be used in tests (basically, wrapper methods for long wait).

In addition, I have the following code:

class Helpers
  include Singleton
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::NumberHelper
end

def helper
  Helper.instance
end

. Helper , Helper.instance.

, . , ?

1

include , :

require 'spec_helper'

describe MyMailer do
  include ApplicationHelper
  include MailersHelper
  include Helpers
  ...
end

2

helpers.rb :

require 'spec_helper'

module Helpers
  include MailersHelper
  include ApplicationHelper
  ...

end

RSpec.configure { |c| c.include Helpers }

require_relative './helpers'

3

, ApplicationHelper MailersHelper Helpers, :

RSpec.configure do |c| 
  c.include Helpers
  c.include ApplicationHelper
  c.include MailersHelper
end

:

  • RSpec.configure (helpers.rb), require_relative . , , , , ? ( , spec_helper.rb).

  • , , , ( Mailers). , ?

- /, ( /), require it relative_require, , , , , RSpec.configure... oh, boy

+4
1

spec_helper.rb. include .

RSpec.configure do |config|
  config.include MailersHelper, type: :mailer
end

spec/mailers MailersHelper.

,

RSpec.describe FooConstant, type: :mailer do
  # ... etc
end

, spec/mailers, .

+2

Source: https://habr.com/ru/post/1548742/


All Articles