您如何测试公开为helper_method的Rails控制器方法? [英] How do you test a Rails controller method exposed as a helper_method?
本文介绍了您如何测试公开为helper_method的Rails控制器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
似乎无法从ActionView :: TestCase访问它们
They don't seem to be accessible from ActionView::TestCase
推荐答案
是的,辅助方法未在视图测试中公开-但可以在功能测试中对其进行测试.并且由于它们是在控制器中定义的,因此这是测试它们的正确位置.您的辅助方法可能定义为private
,因此您必须使用Ruby元编程来调用该方法.
That's right, helper methods are not exposed in the view tests - but they can be tested in your functional tests. And since they are defined in the controller, this is the right place to test them. Your helper method is probably defined as private
, so you'll have to use Ruby metaprogramming to call the method.
app/controllers/posts_controller.rb:
app/controllers/posts_controller.rb:
class PostsController < ApplicationController
private
def format_something
"abc"
end
helper_method :format_something
end
test/functional/posts_controller_test.rb:
test/functional/posts_controller_test.rb:
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
test "the format_something helper returns 'abc'" do
assert_equal 'abc', @controller.send(:format_something)
end
end
这篇关于您如何测试公开为helper_method的Rails控制器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文