---
title: "ActionController::TestSession的bug？"
author: "robbin"
date: "2007-02-12T22:54:29Z"
tags: []
slug: "actioncontroller-testsession-bug"
draft: false
lastmod: "2007-02-12T22:54:29Z"
id: 54058
url: "https://robbin.iteye.com/blog/54058"
---

我在看《Agile Web Development with rails》这本书第二版的单元测试这一章，其中在functional test这一部分，rails提供了一些模拟浏览器请求的get，post等方法，其支持参数格式例如:

get :action, {params}, {session}, {flash}

然后这本书第202页给出一个指定Session的例子为：


```ruby
  def test_index_with_user
    get :index, {}, {:user_id => users(:dave).id}
    assert_equal(users(:dave).id, session[:user_id])
    assert_response :success
    assert_template "index"
  end
```


但是实际跑这个测试，却发现程序无论如何拿不到session，十分纳闷，在程序里面PP.pp session，发现TestSession对象的属性@attributes{:user_id => 1}已经有了，但是就是拿不出来，奇怪，实在奇怪。

无奈只得去趴rails的ActionController::TestSession的源代码，终于在test_processor.rb里面找到，第291行：


```ruby

    def [](key)
      data[key.to_s]
    end

    def []=(key, value)
      data[key.to_s] = value
    end

```


嗯，用string做key，没有提供用symbol做key的取法，于是把上面测试代码中get这一行修改，原来是：

```ruby
get :index, {}, {:user_id => users(:dave).id}
```

现在改为：

```ruby
 get :index, {}, {"user_id" => users(:dave).id}
```


测试程序顺利通过！

问题解决了，不过我不知道这算不算一个bug啊？大家说呢？我是不是应该提交一个issue过去啊？