2007-04-13
献给初学者: 实现带有权限校验情况下的功能测试
Agile with rails 一书中详细的描述了如何进行测试,
但是在开发中,我们或许会遇到下面的情况:
用户必须登陆系统才能进行其他工作;
用户信息保存在 session 里;
登陆工作是在 单独的 controller里(如 LoginController).
这样的情况下, 当我们想对 SearchController 进行功能测试的时候,
会遇到没有权限访问action的情况.
但是我们又不能在 SearchContrller_test 里调用 LoginController_test,
那么如何处理呢?
以下是我的解决办法:
方法很简单,就是 在 get(post) 方法里,设置 session的数值.
如果还有更好的办法希望能共享出来, thanks!
但是在开发中,我们或许会遇到下面的情况:
用户必须登陆系统才能进行其他工作;
用户信息保存在 session 里;
登陆工作是在 单独的 controller里(如 LoginController).
这样的情况下, 当我们想对 SearchController 进行功能测试的时候,
会遇到没有权限访问action的情况.
但是我们又不能在 SearchContrller_test 里调用 LoginController_test,
那么如何处理呢?
以下是我的解决办法:
def setup
@controller = ReportController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
test_user
end
def test_user
# 这个 @tom 是 fixtures 中的数据
@user = User.find(:first, :conditions=>['id=?', @tom.id])
assert_not_nil @user
end
def test_index
#权限校验的规则就是如果 session[:app_user] not null, 就说明登陆了
get :index,{}, :app_user=>@user
assert_response :success
end
方法很简单,就是 在 get(post) 方法里,设置 session的数值.
如果还有更好的办法希望能共享出来, thanks!
- 浏览: 144943 次

- 详细资料
搜索本博客
我的相册
tdd
共 3 张
共 3 张
最新评论
-
网站的静态页面生成方案
笑 喘 写道yananay 写道 把对 index.do 请求后的结果保存成一个 ...
-- by tianmo2008 -
网站的静态页面生成方案
我曾经尝试过在ASP.NET中做这样的事情,用某户访问某个页面时,先从配置里读取 ...
-- by 活靶子 -
网站的静态页面生成方案
对于CMS静态发布,我用的是用Veocity模板去 生产的,根据一定的业务逻辑去 ...
-- by pablo3518 -
聊聊大连新的残疾人制度
政府张口要钱,什么理由已经不重要了。。。
-- by issppt -
请先不要讨论细节好吗
[quote="ayis"][quote="kimmking"][quote=" ...
-- by gzstyxb






评论排行榜