2013-05-17 29 views
0

我試圖通過rspec運行一些測試,並且我不斷地收到錯誤的流量不被重新路由。它特別發生在PUT和DELETE請求上。我沒有收到路由錯誤,錯誤的是:PUT請求似乎總是路由到Rails

Expected response to be a redirect to http://www.example.com/ but was a redirect to https://www.example.com/users/212

我UsersController看起來有點像這樣:

class UsersController < ApplicationController 
    before_filter :signed_in_user, only: [:edit, :update, :index, :destroy] 

    . 
    . 
    . 

    def update 
    puts "Update called." 

    if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated" 
     sign_in @user 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

,但運行時的RSpec從不打印「更新名爲」,但「顯示調用」總是放在顯示方法中。 signed_in方法如下所示:

def signed_in_user 
    puts "Signed in?: #{signed_in?}" 

    if !signed_in? 

    store_location 
    flash[ :notice ] = "You must sign in to access this page." 
    redirect_to signin_url 
    end 
end 

「登錄」也從不打印。

這裏是的投擲的錯誤之一測試:

describe "for non-signed-in users" do 
    let(:user) { FactoryGirl.create(:user) } 

    describe "in the Users controller" do 
    describe "submitting to the update action" do 
     before { put user_path(user) } 
     specify { response.should redirect_to(signin_path) } 
    end 
    end 
end 

我最好的猜測是,測試不尊重呼叫前「放」關鍵詞的,但我不知道爲什麼,或者那裏發生了什麼。我使用的軌道3.2.13版本和RSpec護欄版本2.11.0

編輯:如果我在測試語句的開頭插入puts user(但let語句後),我得到一個錯誤:

undefined local variable or method `user' for #<Class:0x007fd4943743d8>

+0

我絕對不是RSpec的權威人士,但是我見過的每個例子都做得更像:'put:update,id:@user,user:{some:「params」}' –

+0

剛剛試過'put :update,id:user.id,user:user',同樣的錯誤不幸 編輯:看着錯誤的錯誤,這給了我一個錯誤的參數錯誤。 (預期的URI對象或URI字符串) –

回答

0

如果有人好奇,我發現我在某些方面有錯誤,雖然更好的解釋會很好。

基本上我強迫rails應用程序使用ssl,由於某種原因rspec確實不喜歡那樣。註釋掉ssl行解決了所有問題。