如何添加将调用辅助方法的摩卡期望?

发布于 2024-10-07 19:05:28 字数 515 浏览 1 评论 0原文

我正在将一个方法从控制器转移到助手中;现在将从视图中调用该方法。以前,在我的控制器

def show
  @things = gather_things
end

和现在的功能测试中

test "show assigns things" do
  get :show
  assert_equal GATHERED_THINGS, assigns(:things)
end

,gather_things 位于助手中并从视图中调用。我对助手进行了单元测试,以确保它返回正确的值,但我希望我的功能测试断言它被调用。我已经尝试过,

test "show calls gather_things" do
  @controller.expects(:gather_things)
  get :show
end

但那不起作用。我应该调用 expects(:gather_things) 来做什么?

I'm moving a method from a controller into a helper; the method will now be called from the view. Previously, in my controller I had

def show
  @things = gather_things
end

and in my functional test I had

test "show assigns things" do
  get :show
  assert_equal GATHERED_THINGS, assigns(:things)
end

now, gather_things lives in the helper and is called from the view. I have a unit test for the helper which makes sure that it returns the right values, but I want my functional test to assert that it gets called. I've tried

test "show calls gather_things" do
  @controller.expects(:gather_things)
  get :show
end

but that doesn't work. What should I be calling expects(:gather_things) on?

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

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

发布评论

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

评论(1

旧街凉风 2024-10-14 19:05:28

如果您已将代码从控制器移至视图中,那么它实际上已移出功能测试的范围。

我不知道正确的类来满足您的期望...但您可能可以通过在“gather_things”方法中执行以下操作来弄清楚:logger.error self.class.name - 它会为您将类名吐出到日志中。然后你就可以把你的期望寄托在这门课上了。

现在我们来到了问题的关键......您是否应该将该代码移至视图中?

我的意见是你不应该。设置模型对象的集合正是控制器代码中应该执行的操作 - 如果您稍后决定要在 CSV 文件中或作为 RESTful xml API 显示相同的数据,该怎么办? - 无论您使用什么视图,您仍然需要实例化同一组对象。所以我的最终建议是将该方法再次移回控制器中它所属的位置。

编辑:以下建议现已过时,与该用户无关,但可能对其他人有用

如果您正在使用 rspec 进行测试 - 它 在功能测试期间实际上不会渲染视图(除非您专门打开它),因此任何代码调用的表单视图将不会执行。

如果您想测试视图调用某些内容,您需要打开它,或者在视图测试中测试它。

If you have moved the code out of the controller into a View then it has in fact moved out of the purview of the functional test.

I don't know the correct class to put your expects on... but you can probably figure it out by, in the method "gather_things" do something like: logger.error self.class.name - which will spit out the class name into the log for you. Then you can put your expects onto that class.

Now we come to the crux of the matter... should you have moved that code into the view?

My opinion is that you should not. Setting up a gathering of model-objects is precisely what is supposed to go in the controller code - what if you later decide you want to display the same data in a CSV file, or as a RESTful xml API? - you still need the same set of objects instantiated regardless of what view you use. So my Ultimate Recommendation would be to move that method back into the controller again where it belongs.

Edit: following is now obsolete advice and not relevant to this user, but might be to others

If you are testing using rspec - it doesn't actually render the views during a functional test (unless you specifically switch that on), and therefore any code called form views will not execute.

If you want to test that the View calls something, you will either need to turn that on, or test it in your view-tests.

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