I've been working on trying to fix tests after migrating from
authlogic to devise for some time now. I seem to have reached an
impasse with a functional test. This problem seems to be specific to
this particular controller and only for put / post requests. Whenever
a put or post request is performed the controller always redirects to
the sign in page, despite the fact the user seems to be logged in.
Also, though the tests fail the code seems to work fine. This is
using devise 1.0.10, warden 1.0.3, rails 2.3.11, tested with ruby
1.8.7 and ruby 1.9.2. I've extracted a portion of the test:
context "when signed in" do
setup do
@user = Factory.create(:user)
@event = Factory.create(:event_listing)
sign_in @user
end
context "on GET to edit" do
setup do
# :edit is protected by :authenticate_user!
get :edit, :city=>@event.city.url, :id=>@
event.id
end
should respond_with :success
should render_template 'edit'
end
context "on PUT to update with no changes" do
setup do
# :update is protected by :authenticate_user!
put :update, :city=>@event.city.url, :id=>@
event.id
end
should "debug" do
puts "\nUser active?: #{@user.active?}"
puts "Flash: #{flash.inspect}"
end
should respond_with :redirect
should redirect_to('edit url')
{{:action=>:edit, :id=>@
event.id}}
end
end
Here is the run:
$ ruby -I test/ test/functional/events_controller_test2.rb
Loaded suite test/functional/events_controller_test2
Started
..
User active?: true
Flash: {}
.F.
Finished in 4.640287 seconds.
1) Failure:
test: when signed in on PUT to update with no changes should redirect
to . (EventsControllerTest)
[shoulda (2.11.3) lib/shoulda/assertions.rb:59:in `assert_accepts'
shoulda (2.11.3) lib/shoulda/context.rb:324:in
`__bind_1298855227_319661'
shoulda (2.11.3) lib/shoulda/context.rb:382:in `call'
shoulda (2.11.3) lib/shoulda/context.rb:382:in `test: when signed
in on PUT to update with no changes should redirect to . ']:
Expected response to be a redirect to <
http://test.host/foo-8/events/
10679/edit> but was a redirect to <
http://test.host/users/sign_in?
unauthenticated=true>.
5 tests, 7 assertions, 1 failures, 0 errors
It should also be noted that if I change the authenticate_user! line
to:
before_filter :authenticate_user!, :except => [:update]
The tests pass correctly, though obviously I do not wish to have this
action unprotected. Also I have shown that there are no flash
messages and the user.active? is true. So what are the next steps to
resolve this?