在 Rspec 中添加控制器宏 [英] Adding Controller Macros in Rspec

查看:46
本文介绍了在 Rspec 中添加控制器宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Rspec 定义一些控制器宏.我使用 rails 3 并在 spec/support/macros/controller_macros.rb 中定义了我的宏,该文件如下所示:

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)

所以在我的控制器规范中,我只是在我的管理测试中调用 login_admin 但是当我使用这个方法时我得到

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)

起初我认为没有包含 controller_macros.rb,但是当我向文件添加puts"但表明文件至少正在执行时.

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.

我看不出我的设置有什么问题,将 login_admin 方法复制到 describe 块中工作正常,所以我不确定它有什么问题.

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.

推荐答案

也许我迟到了,但对于新人来说.

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.

这篇关于在 Rspec 中添加控制器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆