我正在对控制器方法进行测试,如下所示:
it "should assigns an instance var @items including my item" do
@item = FactoryGirl.create(:item)
get :index
response.should include(@item)
end
控制器方法是:
def index
@Item = Item.select_items
end
其中 select_items 是 Items 模型中的范围方法:
scope :select_items, where(Items.arel_table[:select_field].not_eq(nil))
在开发中,这工作正常。然而,在测试中,我得到
- 在控制台中测试控制器方法本身时出现 500 错误
- 当我在控制台中简单地测试模型方法时没有错误
- 运行规范时永久挂起
在 test.log 中,挂起发生在以下最后一行:
Connecting to database specified by database.yml
Admin Load (0.5ms) SELECT `admins`.* FROM `admins`
(0.6ms) RELEASE SAVEPOINT active_record_1
(0.2ms) SAVEPOINT active_record_1
SQL (1.1ms) INSERT INTO `items` (`various fields')
(0.1ms) RELEASE SAVEPOINT active_record_1
Processing by Admin::ItemsController#index as HTML
Admin Load (0.4ms) SELECT `admins`.* FROM `admins` WHERE `admins`.`id` = 1 LIMIT 1
Rendered admin/items/index.haml within layouts/tabs (0.2ms)
Completed 200 OK in 15ms (Views: 3.9ms | ActiveRecord: 0.4ms)
Item Load (1.2ms) SELECT `items`.* FROM `items` WHERE (`item`.`select_field` IS NOT NULL) LIMIT 25 OFFSET 0
如果我在 rspec 中运行测试,则规范会在此时停止。为什么?