簡體   English   中英

redirect_to:action =>:Rspec中的index

[英]redirect_to :action => :index in Rspec


我嘗試用Google搜索我的問題的確切解決方案,但未找到任何合適的結果,因此我在這里尋求解決方案。

這是重要的信息
Ruby版本ruby 2.2.4p230(2015-12-16修訂版53155)[x86_64-darwin15]
Rails版本Rails 4.2.0
Rspec版本3.5.4

我有一個控制器,它具有一個稱為clearCart的動作,該動作基本上用於清除cart session

這是我的行動

def clearCart
  session[:cart] = nil
  redirect_to :action => :index
end

上述動作的主要目的是測試redirect_to :action => :index

相同動作的路線 (如果需要)

get 'cart/clear' => 'cart#clearCart'

這是Rspec測試,用於測試相同的操作

describe "GET #clearCart" do
  it "should clear cart" do
    get :clearCart
    subject { get :clearCart }
    subject.should redirect_to :action => :index
    # expect(response).to redirect_to(:index)
    # response.should redirect_to :index
    # before { post :clearCart }
    # render 'index'
    # specify { response.should redirect_to('frontend/cart/index') }
  end
end

(評論是我嘗試執行測試的可能方式)

這是我的索引操作 (如果需要)

def index
  render 'frontend/cart/index' ,layout: false
end

我通常會向RELISH尋求指導。

在此先感謝您的幫助。

如果您期望匹配器,則以下代碼應該有效

describe "GET #clearCart" do
  subject { get :clearCart }
  it { is_expected.to redirect_to(action: :index) }
end

萬一匹配者使用

describe "GET #clearCart" do
  before { get :clearCart }
  it { should redirect_to(action: :index) }
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM