我正在使用Rspec(2.13.0)請求規格測試移動站點的登錄api。我有一個login_user方法,可以正確寫入用戶測試數據庫並在db中生成一個auth_token。在Rspec請求規範中訪問會話變量規範
我不認爲auth_token在示例「只測試mobile-login-page」中作爲會話值被正確設置。我可以看到,雖然登錄後發生了重定向,並且發送了正確的auth_token(請參閱login_user方法中的註釋)。
是我的語法不正確(這是我第一次使用請求規範這樣的東西)?使用控制器規格或使用機架測試會更好嗎?
THX提前
def login_user
@user = FactoryGirl.create :user
visit '/arc/signin'
fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'foobar'
click_button 'Log in'
# from test.log showing that auth_token is actually called
# Processing by UsersController#home as HTML
# ESC[1mESC[35mUser Load (0.4ms)ESC[0m SELECT `users`.* FROM `users` WHERE `users`.`auth_token` = 'pCnpG90vIw3qUAoXx3EBsA' LIMIT 1
#
end
it "just testing the mobile-login-page" do
login_user
get '/arc/v1/api/signup-mobile-test'
puts response.body
JSON.parse(response.body)[:auth_token] == (@user.auth_token)
end
在控制器:
def create_user_test
m={}
m[:status]="success"
m[:auth_token]=session[:auth_token]
render :json => m.to_json
end
和這裏的輸出:
Fri Apr 26$ rspec spec/requests/static_page_spec.rb
{"status":"success","auth_token":null}
.....
Finished in 0.80587 seconds
5 examples, 0 failures
你在起訴嗎? – 2013-04-26 19:41:31
不,只是has_secure_password;我可以驗證用戶和auth_token是否在數據庫中正確創建。 – timpone 2013-04-26 19:42:31