spec/controllers 中的测试代码
# -*- encoding : utf-8 -*-
require 'spec_helper'
describe UsersController do
describe "sign in" do
let(:user) { FactoryGirl.create(:user) }
let(:admin_user) { FactoryGirl.create(:admin) }
describe "not admin" do
# 放在这里登陆 session 也无效
#before { sign_in(user) }
it "update another user should redirect to root path" do
sign_in(user)
# p cookies.to_json
# p sessions
put :update, id: user, user: FactoryGirl.attributes_for(:admin)
response.should redirect_to root_url
end
end
end
end
这里可以保证的是,sign_in 方法执行成功了,并且 controller 也做出了响应,进行了登陆的 session 和 cookie 但接下来的 put 测试,cookies, session 都为空,是因为 put 请求又将 session/cookie 信息给 reset 了?
在 controller 的测试中,如何注册 session (登陆) 呢?
由于是 rails 的新手,很多 rails magic 不懂 - -||
https://github.com/wppurking/train_app/blob/master/spec/support/helpers.rb https://github.com/wppurking/train_app/blob/master/spec/controllers/users_controller_spec.rb