active_decoratorがtest_unitでもrspecと同じくそのままじゃhelperを使ったdecoratorのtestが書けないんだけど下記のようにすれば動く。(active_decoratorのREADMEに書いてあるdecoratorのテストの例)

# test/test_helper.rb:
class ActiveSupport::TestCase
  include ActionView::TestCase::Behavior
end
# test/decorators/user_decorater_test.rb:
require 'test_helper'

class UserDecoratorTest < ActiveSupport::TestCase
  def setup
    ActiveDecorator::ViewContext.current = controller.view_context
    @user = ActiveDecorator::Decorator.instance.decorate users(:jean)
  end 

  test 'full_name' do
    assert_equal 'Jean Valjean', @user.full_name
  end

  test 'link' do
    assert_equal '<a href="http://jeanvaljean.com">Jean Valjean</a>', @user.link
  end
end
$ rake test test/decorators/user_decorator_test.rb
Run options: --seed 37850

# Running:

..

Finished in 0.032828s, 60.9236 runs/s, 60.9236 assertions/s.

2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

全部のコードはこちら

もっときれいな書き方があったらぜひ知りたいです。

Comments


Option