2013-08-04 41 views
6

我正在嘗試編寫測試和應用程序代碼以將已登錄到root_path的用戶重定向,如果他們嘗試創建用戶或訪問NEW用戶路徑。Rails教程Ch。 9練習6:預期的響應是<redirect>,但是<200>

下面是我寫在user_pages_spec.rb測試:

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

     describe "using a 'new' action" do 
     before { get new_user_path } 
     specify { response.should redirect_to(root_path) } 
     end 

     describe "using a 'create' action" do 
     before { post users_path } 
     specify { response.should redirect_to(root_path) } 
     end   
    end 

UsersController:

class UsersController < ApplicationController 
    before_action :unsigned_in_user, only: [:create, :new] 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     sign_in @user 
      flash[:success] = "Welcome to the Sample App!" 
      redirect_to @user 
    else 
      render 'new' 
    end 
    end 

    private 
    # Before filters 

    def user_params 
     params.require(:user).permit(:name, :email, :password, 
           :password_confirmation) 
    end 

    def unsigned_in_user 
     puts signed_in? 
     redirect_to root_url, notice: "You are already signed in." unless !signed_in? 
    end 
end 

puts signed_in?返回false。我認爲這是問題,因爲我希望它會返回true。以下是使用rspec運行測試後的錯誤。任何幫助表示讚賞。

Failures: 

    1) User pages for signed in users using a 'create' action 
    Failure/Error: before { post users_path } 
    ActionController::ParameterMissing: 
     param not found: user 
    # ./app/controllers/users_controller.rb:52:in `user_params' 
    # ./app/controllers/users_controller.rb:20:in `create' 
    # ./spec/requests/user_pages_spec.rb:162:in `block (4 levels) in <top (required)>' 

    2) User pages for signed in users using a 'new' action 
    Failure/Error: specify { response.should redirect_to(root_path) } 
     Expected response to be a <redirect>, but was <200> 
    # ./spec/requests/user_pages_spec.rb:158:in `block (4 levels) in <top (required)>' 

內sessions_helper.rb文件:

def signed_in? 
    !current_user.nil? 
end 

在規格/支持/ utilities.rb:

def sign_in(user, options={}) 
    if options[:no_capybara] 
    # Sign in when not using Capybara. 
    remember_token = User.new_remember_token 
    cookies[:remember_token] = remember_token 
    user.update_attribute(:remember_token, User.encrypt(remember_token)) 
    else 
    visit signin_path 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 
    end 
end 
+0

發佈signed_in的代碼?方法。既然它正在回報你沒有想到的東西,那麼問題可能就在於此。添加了 – amb110395

+0

。我是新來的鐵軌,但我想知道是否signed_in事實?在sessions_helper.rb中定義,我試圖在用戶控制器(而不是會話控制器)中使用它會帶來問題.. – IkegawaTaro

+0

肯定會導致問題,除非您在ApplicationController中包含SessionHelper。是你做的嗎? – amb110395

回答

4

是你能得到你的測試通過?

如果你沒有,我今天有同樣的問題,因爲你,並能得到測試通過使兩個變化,以測試通過 - 發帖時傳遞user散列的,並且使用了no_capybara選項sign_in方法,因爲getpost不是水豚方法,我認爲如果我們在相同的測試中從水豚切換到非水豚方法,RSpec不會像我們預期的那樣表現。

describe "for signed-in users" do 
    let(:user) { FactoryGirl.create(:user) } 
    before { sign_in user, no_capybara: true } 

    describe "using a 'new' action" do 
    before { get new_user_path }  
    specify { response.should redirect_to(root_path) } 
    end 

    describe "using a 'create' action" do 
    before do 
     @user_new = {name: "Example User", 
        email: "[email protected]", 
        password: "foobar", 
        password_confirmation: "foobar"} 
     post users_path, user: @user_new 
    end 

    specify { response.should redirect_to(root_path) } 
    end 
end 
+1

不錯!我在第一次測試中添加了no_capybara:true選項,現在都通過了。我沒有通過創建用戶散列,但現在通過。我想這不是一個要求...即從microposts測試:描述「提交到創建操作」在{post microposts_path} 指定{expect(response).to redirect_to(signin_path)} end – IkegawaTaro

+1

之前做 哦,這是有道理的,因爲用戶散列只會是如果Rails實際執行創建方法,這就是我們所要阻止的方法。謝謝 - 我們都在填補彼此的空白:) – najwa

3

相同的答案najwa,但是我用了FactoryGirl 用戶屬性方法Rails的,以避免重複:

describe "using a 'create' action" do 
    before { post users_path, user: user.attributes } 

    specify { response.should redirect_to(root_path) } 
end 

有助於保持從測試代碼分離的數據。

相關問題