views:

323

answers:

2

They don't seem to be accessible from ActionView::TestCase

A: 

Indeed they're not. The view tests are specifically for the views. They don't load the controllers.
You should mock this method and make it return whatever is appropriate depending of your context.

Damien MATHIEU
"You should mock this method and make it return whatever is appropriate" - well I actually want to test the method :-)
Teflon Ted
Yeah but testing an helper method inside of a view isn't the appropriate way of doing so :)
Damien MATHIEU
"testing an helper method inside of a view isn't the appropriate way of doing so" - then why does the Rails scaffold generate helper tests based on ActionView::TestCase ? :-( and what is the proper way? all I want is to test this method. thanks.
Teflon Ted
Yes but this method is primarily a controller method. You should test it in your controller/lib.
Damien MATHIEU
+1  A: 

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:

class PostsController < ApplicationController

  private

  def format_something
    "abc"
  end
  helper_method :format_something
end

test/functional/posts_controller_test.rb:

require 'test_helper'

class PostsControllerTest < ActionController::TestCase
  test "the format_something helper returns 'abc'" do
    assert @controller.send(:format_something) == "abc"
  end
end
Jonathan Julian