Метод assigns
должен позволять запрашивать значение @selected_trust
.Чтобы утверждать, что его значение равно «test» следующим образом:
assert_equal 'test', assigns('selected_trust')
При наличии контроллера foo_controller.rb
class FooController < ApplicationController
before_filter :get_trust_from_subdomain
def get_trust_from_subdomain
@selected_trust = "test"
end
def index
render :text => 'Hello world'
end
end
можно написать функциональный тест следующим образом в foo_controller_test.rb
:
class FooControllerTest < ActionController::TestCase
def test_index
get :index
assert @response.body.include?('Hello world')
assert_equal 'test', assigns('selected_trust')
end
end
Относительно комментария: обратите внимание, что фильтр можно поместить в ApplicationController
, и тогда любой производный контроллер также унаследует это поведение фильтра:
class ApplicationController < ActionController::Base
before_filter :get_trust_from_subdomain
def get_trust_from_subdomain
@selected_trust = "test"
end
end
class FooController < ApplicationController
# get_trust_from_subdomain filter will run before this action.
def index
render :text => 'Hello world'
end
end