Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Controller Macros in Rspec

Im trying to define some controller macros for Rspec. Im using rails 3 and have my macros defined in spec/support/macros/controller_macros.rb, that 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 spec i just call login_admin in my admin tests but when ever i use the method i get

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

At first I assumed that controller_macros.rb wasn't being included but when I added a "puts" to the file but that showed the file was at least being executed.

I can't see anything wrong with my setup and copying the login_admin method into the describe block works fine so im not sure whats wrong with it.

like image 608
Arcath Avatar asked Jul 22 '26 19:07

Arcath


1 Answers

Maybe I am late to that, but for new comers.

Here is a good examples of using macros:

http://osmose.6spot.com.br/2011/01/rails-resource-routing-spec-w-rspec/

when you include a module it's methods are visible inside examples.

But when you extend the module, it's methods are only visible outside examples.

It gives you ways to compose your macros for each situation.

like image 153
feroult Avatar answered Jul 24 '26 17:07

feroult



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!