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
def index
render :text => 'Hello world'
end
end
ApplicationController
中,测试应该仍然有效。FooController
只是一个说明性的例子。此外,任何直接或间接继承ApplicationController
的控制器都将继承此过滤器行为,并且您可以在任何相关的控制器功能测试中进行测试。 - Richard CookApplicationController
中定义的before_filter
最简单的方法是将它们作为派生控制器的测试的一部分来测试吗? - pjmorseApplicationController
派生,仅在测试代码的上下文中可用,专门用于此目的。 - Richard Cook