如何使用rspec在设计中测试json登录

obo*_*obo 7 authentication json ruby-on-rails devise rspec2

我正在尝试使用设计构建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
Run Code Online (Sandbox Code Playgroud)

我的路线:

devise_for :users, :controllers => {:sessions => "sessions"}
resources :users
Run Code Online (Sandbox Code Playgroud)

然后我有以下规范来测试这个过程:

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
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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?
Run Code Online (Sandbox Code Playgroud)

[编辑]似乎功能还可以,但只有测试被打破; 因为如果我运行这个小脚本:

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
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

"{\"error\":\"Success\",\"user\":{\"email\":\"user@test.com\",\"name\":\"First User\"}}"
Run Code Online (Sandbox Code Playgroud)

这是预期的.

obo*_*obo 7

我不知道它是否是最好的解决方案,但这是我如何通过设备上的webservices成功测试我的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
Run Code Online (Sandbox Code Playgroud)

有了它,我可以创建一个新用户,注销,再次登录,编辑帐户和删除帐户.一切都通过json中的5个webservices.这个执行顺序允许每次都进行测试,但我认为在广告系列结束时缺少一个真正的回滚过程.