Tôi đang cố gắng xây dựng quy trình đăng nhập đường ray với ý tưởng cho phép người dùng đăng nhập/đăng xuất thông qua ứng dụng dành cho thiết bị di động.Làm thế nào để kiểm tra đăng nhập json trong lập với rspec
Tôi tạo ra một SessionsController như thế:
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html { super }
format.json {
render :status => 200, :json => { :error => "Success", :user => resource}.to_json
}
end
end
def destroy
super
end
end
tuyến đường của tôi:
devise_for :users, :controllers => {:sessions => "sessions"}
resources :users
Sau đó, tôi có spec sau để kiểm tra quá trình:
require 'spec_helper'
describe SessionsController do
describe "POST 'signin'" do
before (:each) do
@user = Factory(:user)
end
it "should login with json request" do
@expected = @user.to_json
post :create, :user => {:email => '[email protected]', :password => 'please'}, :content_type => 'application/json', :format => :json
response.body.should == @expected
end
end
end
Và tôi nhận được lỗi sau:
Failure/Error: post :create, :user => {:email => '[email protected]', :password => 'please'}, :content_type => 'application/json', :format => :json
AbstractController::ActionNotFound:
Could not find devise mapping for path "https://stackoverflow.com/users/sign_in.json?content_type=application%2Fjson&user%5Bemail%5D=user%40test.com&user%5Bpassword%5D=please".
Maybe you forgot to wrap your route inside the scope block?
[EDIT] Dường như chức năng là ok nhưng chỉ thử nghiệm bị hỏng; bởi vì nếu tôi chạy script nhỏ này:
require 'rest_client'
require "active_support/core_ext"
RestClient.post "http://localhost:3000/users/sign_in", {:user => {:email => '[email protected]', :password => 'please'}}.to_json, :content_type => :json, :accept => :json
tôi nhận được kết quả sau:
"{\"error\":\"Success\",\"user\":{\"email\":\"[email protected]\",\"name\":\"First User\"}}"
đó là một trong những dự kiến.
có vẻ như localhost: 3000 cần được bật cho thử nghiệm? – Karan
Có: Tôi sử dụng bảo vệ để xem qua các sửa đổi và chạy thử nghiệm trong quá trình phát triển, vì vậy localhost: 3000 luôn được bật. – obo