Reputation: 1165
I tried to google for the exact solution for my problem but didn't found any appropriate result, so I'm asking for the solution here.
Here is the important information
Ruby Version : ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin15]
Rails Version : Rails 4.2.0
Rspec Version : 3.5.4
I've a controller which has an action called clearCart
which basically functions to clearing the cart session
Here is my action
def clearCart
session[:cart] = nil
redirect_to :action => :index
end
The main aim for the above action is to test redirect_to :action => :index
.
Routes for the same action (if needed)
get 'cart/clear' => 'cart#clearCart'
Here is the Rspec test to test the same action
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
(comments are the possible way which I tried to execute the test)
Here is my index action (if needed)
def index
render 'frontend/cart/index' ,layout: false
end
I usually refer RELISH for guidance.
Thanks in advance for help.
Upvotes: 0
Views: 448
Reputation: 2020
If you have expect matchers the following code should work
describe "GET #clearCart" do
subject { get :clearCart }
it { is_expected.to redirect_to(action: :index) }
end
In case of shoulda matchers use
describe "GET #clearCart" do
before { get :clearCart }
it { should redirect_to(action: :index) }
end
Upvotes: 4