ruby-on-rails – 在Rails中测试ApplicationController before_f
发布时间:2020-12-17 01:57:58 所属栏目:百科 来源:网络整理
导读:我有一个应用程序检测请求上的子域并将结果设置为变量. 例如 before_filter :get_trust_from_subdomaindef get_trust_from_subdomain @selected_trust = "test"end 我怎么用Test :: Unit / Shoulda来测试呢?我没有看到进入ApplicationController并查看设置
我有一个应用程序检测请求上的子域并将结果设置为变量.
例如 before_filter :get_trust_from_subdomain def get_trust_from_subdomain @selected_trust = "test" end 我怎么用Test :: Unit / Shoulda来测试呢?我没有看到进入ApplicationController并查看设置的方法…… 解决方法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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |