如何在 Rails 的超类中存根 before_filter ?

发布于 2024-08-22 18:24:59 字数 816 浏览 4 评论 0原文

我在 RSpec 中使用 RR 进行模拟和存根,并且遇到过一种情况,我想对设置一些实例变量的控制器的超类中的方法进行存根。我可以弄清楚如何存根方法调用,如果我调试,我可以看到我的存根块被调用,但我无法将块中的实例变量传播到我正在测试的类中。

只是为了分解它:

class A < ApplicationController
  before_filter :bogglesnap

  def bogglesnap
    @instancevar = "totally boggled"
  end
end

class B < A
  def do_something_with_instance
    if @instancevar 
      ....
    else
      ....
    end
  end
end

这是基本设置,因此在我对控制器 B 的测试中,我想从 A 中删除 bogglesnap 方法,以将 @instancevar 设置为我想要的东西。我只是不知道该怎么做。

我已经尝试过 RR 的 instance_of 存根并只是存根控制器定义:

stub.instance_of(A).bogglensap { @instancevar = "known value" }
stub(controller).bogglesnap { @instancevar = "known value" }

但这两个似乎都不起作用,嗯,它们不起作用:)

有没有人知道如何存根该方法调用并它设置了实例变量吗?我假设它与块运行的上下文有关,但我希望有人以前遇到过类似的事情。

谢谢

I'm using RR for mocking and stubbing in RSpec, and I've run across a situation where I'd like to stub a method from a super class of a controller that sets some instance variables. I can work out how to stub the method call and if I debug I can see that my stubbed block is called, but I cannot get the instance variables in the block to propagate into the class I'm testing.

Just to break it down :

class A < ApplicationController
  before_filter :bogglesnap

  def bogglesnap
    @instancevar = "totally boggled"
  end
end

class B < A
  def do_something_with_instance
    if @instancevar 
      ....
    else
      ....
    end
  end
end

That's the basic setup, and so then in my tests for controller B I'd like to stub out the bogglesnap method from A to set @instancevar to something I want. I just can't figure out how to do it.

I've tried RR's instance_of stubbing and just stubbing out the controller definition :

stub.instance_of(A).bogglensap { @instancevar = "known value" }
stub(controller).bogglesnap { @instancevar = "known value" }

but neither of these seem to work, well, they don't work :)

Does anyone have any pointers on how you should be able to stub that method call out and have it set instance variables? I'm assuming it has to do with the context in which the block is run but am hoping someone has run across something like this before.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港巷口少年归 2024-08-29 18:24:59

您可以通过调用对象实例并将其设置为您想要的任何内容来使用instance_variable_set方法,就像这样

 controller.instance_variable_set("@instancevar", "known value")

如果您想在规范或调试中获取实例变量的值, 。

 controller.instance_variable_get("@instancevar")

和类似的需要注意的是,instance_variable_set 和instance_variable_get 方法不仅适用于控制器,而且适用于 ruby​​ 提供的所有对象 。事实上,这两种方法在 Rails magic 中发挥着重要作用:)

You can use instance_variable_set method by calling on the object instance and set it to whatever you want, like so

 controller.instance_variable_set("@instancevar", "known value")

and similarly, if you ever want to fetch the value of an instance variable in your spec or debug or do something else from outside the class then you can get the value by doing

 controller.instance_variable_get("@instancevar")

Mind you, instance_variable_set and instance_variable_get methods are available not only to controllers but all objects as it is provided by ruby. Infact, these two methods play an important role in rails magic :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文