Adding Controller Macros to Rspec

I am trying to define some controller macros for rspec. Im using rails 3 and my macros are defined in spec / support / macros / controller_macros.rb, this file looks like this:

module ControllerMacros
    def self.login_admin
        #code
    end
end

in my spec helper i have:

config.include(ControllerMacros, :type => :controller)

So, in my controller specification, I just call login_admin in my admin tests, but when I ever use a method, I get

undefined local variable or method `login_admin' for #<Class:0xb6de4854> (NameError)

At first I assumed that controller_macros.rb was not included, but when I added "puts" to the file, but showed that the file was at least executed.

I don’t see anything wrong with my setup and copying the login_admin method to the description block works fine, so I'm not sure what is wrong with it.

+3
3

Try

ControllerMacros.login_admin

self .

+2

:

? RSpec

login_admin, ControllerMacros, RSpec login_admin

:

spec/support/macros/controller_macros.rb

module ControllerMacros
    def login_admin
        #code
    end
end

Rspec

config.include(ControllerMacros, :type => :controller)

+1

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


All Articles