gpt4 book ai didi

ruby-on-rails - 如何使用 rspec 在设计中测试 json 登录

转载 作者:行者123 更新时间:2023-12-03 13:32:23 24 4
gpt4 key购买 nike

我正在尝试使用设计构建一个 rails 登录过程,该过程将允许用户通过移动应用程序登录/注销。

我创建了一个这样的 SessionsController :

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

我的路线:
devise_for :users, :controllers => {:sessions => "sessions"}
resources :users

然后我有以下规范来测试这个过程:
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 => 'user@test.com', :password => 'please'}, :content_type => 'application/json', :format => :json
response.body.should == @expected
end

end

end

我收到以下错误:
Failure/Error: post :create, :user => {:email => 'user@test.com', :password => 'please'}, :content_type => 'application/json', :format => :json
AbstractController::ActionNotFound:
Could not find devise mapping for path "/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?

[编辑]
看起来功能还可以,但只有测试被破坏了;因为如果我运行这个小脚本:
require 'rest_client'
require "active_support/core_ext"

RestClient.post "http://localhost:3000/users/sign_in", {:user => {:email => 'user@test.com', :password => 'please'}}.to_json, :content_type => :json, :accept => :json

我得到以下结果:
"{\"error\":\"Success\",\"user\":{\"email\":\"user@test.com\",\"name\":\"First User\"}}"

这是预期的。

最佳答案

我不知道这是否是最好的解决方案,但这是我通过 webservices on devise 成功测试我的 json 登录过程的方法:

require 'spec_helper'
require 'rest_client'
require 'active_support/core_ext'

describe "sign up / sign out / sign in edit / cancel account" do

before (:each) do
@user = {:name => "tester", :email =>"tester@test.biz"}
end

describe "POST 'sign up'" do

it "should sign up with json request" do
response_json = RestClient.post "http://localhost:3000/sign_up", {:user=>{:name=>"tester", :email => "tester@test.biz", :password => "FILTERED", :password_confirmation => "FILTERED"}}.to_json, :content_type => :json, :accept => :json
response = ActiveSupport::JSON.decode(response_json)
response["response"].should == "ok"
response["user"].should == @user.as_json
@@token = response["token"]
@@token.should_not == nil
end

end


describe "DELETE 'sign out'" do

it "should logout with json request" do
response_json = RestClient.delete "http://localhost:3000/users/sign_out?auth_token=#{@@token}", :accept => :json
response = ActiveSupport::JSON.decode(response_json)
response["response"].should == "ok"
end

end


describe "POST 'sign in'" do

it "should login with json request" do
response_json = RestClient.post "http://localhost:3000/users/sign_in", {:user => {:email => "tester@test.biz", :password => "FILTERED"}}.to_json, :content_type => :json, :accept => :json
response = ActiveSupport::JSON.decode(response_json)
response["response"].should == "ok"
response["user"].should == @user.as_json
@@token = response["token"]
@@token.should_not == nil
end

end


describe "PUT 'edit'" do

it "should edit user name with json request" do
response_json = RestClient.put "http://localhost:3000/users?auth_token=#{@@token}", {:user => {:name => "tester2", :email => "tester@test.biz", :current_password => "FILTERED"}}.to_json, :content_type => :json, :accept => :json
response = ActiveSupport::JSON.decode(response_json)
response["response"].should == "ok"
response["user"]["email"].should == "tester@test.biz"
response["user"]["name"].should == "tester2"
@@token = response["token"]
@@token.should_not == nil
end

end


describe "DELETE 'account'" do

it "should logout with json request" do
response_json = RestClient.delete "http://localhost:3000/users?auth_token=#{@@token}", :accept => :json
response = ActiveSupport::JSON.decode(response_json)
response["response"].should == "ok"
end

end
end

有了它,我可以创建一个新用户,注销,再次登录,编辑帐户和删除帐户。一切都通过 json 中的 5 个网络服务完成。这种执行顺序允许每次都进行测试,但我认为缺少在事件结束时执行的真正回滚过程。

关于ruby-on-rails - 如何使用 rspec 在设计中测试 json 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7650210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com