Testing view helpers: how to specify a controller?

Consider, for example, the following code:

class ViewHelpersTest < ActionView::TestCase
  should 'generate tag with correct parameters' do
    assert_equal theme_stylesheet_link_tag('style', :media => 'print'),
                 '<link href="/themes/default/stylesheets/style.css" media="print" rel="stylesheet" type="text/css" />'                     
  end
end

current_theme_stylesheet_tagis a view helper that creates a style sheet link tag that points to a css file located inside the current theme directory. You can get the current topic by calling ApplicationController::current_theme.

So, I need to provide a valid controller instance that brings me to my question:

How to specify the exact controller to use when testing view helpers in Rails 3?

Also, if there is a better way to express this test, please let me know. I have little testing experience.

+3
source share
2 answers

, , unit test, . , , .

RSpec , current_theme :

describe ViewHelper do
  it "should generate tag with correct parameters" do
    ApplicationController = double(:current_theme=>"A string")
    helper.my_helper_method("argument").should == "A string"
  end
end

ApplicationController::current_theme, , .

+2

, , . , Rails Guides " HTML ".

, ?

0

Source: https://habr.com/ru/post/1785005/


All Articles