views:

27

answers:

1

I have a spec for testing a controller as below

require 'spec_helper'

describe ProductsController do
setup :activate_authlogic

describe "user not logged in" do

it "should not GET index" do
get :index
response.should redirect_to(login_path)
end

end

describe "user logged in" do

before(:each) do
UserSession.create :username => "rohit", :password => "test123"
end

it "should GET index" do
get :index
response.should redirect_to(products_path)
end

end

end

I have also used this line in spec_helper.rb

require "authlogic/testcase"

The test for "user not logged in passes" but for "user logged in" fails with

'ProductsController user is logged in should GET index' FAILED
expected redirect to "/products", got no redirect
+1  A: 

It seems normal, because You fetch the '/products' url with a logged user. Then He see this page. He is not redirect to the page he see.

Each test are independant. No state are save in previous test.

shingara
why I am getting a no redirect in the "user logged in" example, at least it should get redirected somewhere. I even tried to use redirect_to(login_path) instead of redirect_to(products_path) still I was getting the same error. Why is that?
Rohit
because you ask only the page '/products' if there are no redirect_to something else in this controller it's normal. Put your Production controller if you want more info
shingara
so, basically what are you saying. Should I remove the redirect_to line from the spec
Rohit
sorry @shingara my bad I forgot to check that there are no redirects in my index action of Products controller. You were right this was a normal behavious. Actually I am relatively new to rspec that's why this confusion. Well thanks anyway
Rohit
just had one more doubt:: after doing post :create should we go for a check whether the create was successful or not as we do in our controller like if @product.save "saved" else "not saved" end
Rohit