Helper_Method로 노출 된 Rails 컨트롤러 방법을 어떻게 테스트합니까?
-
20-09-2019 - |
문제
ActionView :: TestCase에서 액세스 할 수없는 것 같습니다.
해결책
맞습니다. 도우미 방법은보기 테스트에서 노출되지 않지만 ~할 수 있다 기능 테스트에서 테스트하십시오. 컨트롤러에 정의되어 있기 때문에 테스트하기에 적합한 장소입니다. 당신의 도우미 방법은 아마도 다음과 같이 정의 될 것입니다 private
, 따라서 방법을 호출하려면 Ruby Metaprogramming을 사용해야합니다.
앱/컨트롤러/posts_controller.rb :
class PostsController < ApplicationController
private
def format_something
"abc"
end
helper_method :format_something
end
테스트/기능/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
다른 팁
개인 메소드에서 보내기를 사용하여 캡슐화를 돌아 다니기 때문에 이것은 어색합니다.
더 나은 접근 방식은 도우미 방법을 /컨트롤러 /관심사 디렉토리의 모듈에 넣고이 모듈에 대해 특별히 테스트를 만드는 것입니다.
앱 컨트롤러/posts_controller.rb의 예 :
class PostsController < ApplicationController
include Formattable
end
앱/컨트롤러/관심사/formattable.rb
module Concerns
module Formattable
extend ActiveSupport::Concern # adds the new hot concerns stuff, optional
def format_something
"abc"
end
end
end
그리고 시험/기능/우려/formattable_test.rb에서
require 'test_helper'
# setup a fake controller to test against
class FormattableTestController
include Concerns::Formattable
end
class FormattableTest < ActiveSupport::TestCase
test "the format_something helper returns 'abc'" do
controller = FormattableTestController.new
assert_equal 'abc', controller.format_something
end
end
당신은 테스트 할 수 있습니다 @controller.view_context
기능/컨트롤러 테스트에서. 이 방법은 내가 알 수있는 한 레일 3, 4 및 5에서 사용할 수 있습니다.
App/Controllers/Application_Controller.rb
class ApplicationController < ActionController::Base
helper_method :current_user
# ...
end
테스트/컨트롤러/application_controller_test.rb
require 'test_helper'
class ApplicationControllerTest < ActionController::TestCase
test 'current_user helper exists in view context' do
assert_respond_to @controller.view_context, :current_user
end
end
컨트롤러 서브 클래스 중 하나를 테스트하지 않으려면 view_context의 메소드의 메소드가 컨트롤러와 동일하고 View Helpers 중 하나가 아닌지 확인하기 위해 테스트 컨트롤러를 작성할 수도 있습니다.
class ApplicationControllerHelperTest < ActionController::TestCase
class TestController < ApplicationController
private
def current_user
User.new
end
end
tests TestController
test 'current_user helper exists in view context' do
assert_respond_to @controller.view_context, :current_user
end
test 'current_user returns value from controller' do
assert_instance_of User, @controller.view_context.current_user
end
end
또는 아마도 요청이있을 때 도우미를 테스트 할 수 있기를 원할 것입니다.
class ApplicationControllerHelperTest < ActionController::TestCase
class TestController < ApplicationController
def index
render plain: 'Hello, World!'
end
end
tests TestController
def with_routing
# http://api.rubyonrails.org/classes/ActionDispatch/Assertions/RoutingAssertions.html#method-i-with_routing
# http://guides.rubyonrails.org/routing.html#connecting-urls-to-code
super do |set|
set.draw do
get 'application_controller_test/test', to: 'application_controller_test/test#index'
end
yield
end
end
test 'current_user helper exists in view context' do
assert_respond_to @controller.view_context, :current_user
end
test 'current_user returns value from controller' do
with_routing do
# set up your session, perhaps
user = User.create! username: 'testuser'
session[:user_id] = user.id
get :index
assert_equal user.id, @controller.view_context.current_user.id
end
end
end
실제로 그들은 아닙니다. 보기 테스트는 특히보기 용입니다. 컨트롤러를로드하지 않습니다.
이 방법을 조롱하고 상황에 따라 적절한 것을 반환해야합니다.
허용 된 답변은 실제로이 방법이 도우미 방법으로 노출되었는지 여부를 테스트하지 않았기 때문에 이것에 대해 조금 고투했습니다.
즉, 우리는 사용할 수 있습니다 #helpers
테스트를위한 프록시를 얻는 방법.
예를 들어:
class FooController < ApplicationController
private
def bar
'bar'
end
helper_method :bar
end
다음과 같이 테스트 할 수 있습니다.
require 'test_helper'
class FooControllerTest < ActionController::TestCase
test 'bar is a helper method' do
assert_equal 'bar', @controller.helpers.bar
end
end