# Testing Problems - Acts as authenticated

**URL:** <https://www.ruby-forum.com/t/testing-problems-acts-as-authenticated/48992>\
**Category:** Rails\
**Created:** [November 28, 2005, 12:25pm UTC](https://www.ruby-forum.com/t/testing-problems-acts-as-authenticated/48992 "2005-11-28T12:25:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [November 28, 2005, 12:25pm UTC](https://www.ruby-forum.com/t/testing-problems-acts-as-authenticated/48992/1 "2005-11-28T12:25:07Z")

</div>

Has anyone successfully run the unit and functional tests that are  
pre-generated with the ‘acts as authenticated’ plugin ?

I’m having huge amounts of problems and it’s not helped by the fact that  
I’m a n00b to Rails and can’t tell if I’ve made some fundamental error  
setting things up.

Sequence of events was

1. Generate users scaffold
2. Generate authenticate controller using plugin
3. alter application.rb so that almost everything requires a logon
4. Run rake

I quickly discovered that there was no way that  
‘test\_should\_not\_rehash\_password’ would run successfully in the unit  
tests, so I abandoned that (commented it out).

On a re-run I found so many errors in other parts of of the app (it’s a  
team project) that I had to run just the ‘users’ functional test on its  
own.

Virtually every test failed.

I thought my mods to application.rb might be the root of the problem,  
but commenting them out got me no further, and replicating that  
functionality within the ‘setup’ method of the functional test didn’t  
help either. I also took a few guesses at things that might be missing  
from the artificial test environment (I used a few includes in other  
words) but again no luck.

I think there is probably something simple I’m missing that will make  
these tests run out of the box, but I’m at a loss to know what it is.

Can anyone help ?

Cheers, Andy

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [November 28, 2005, 12:38pm UTC](https://www.ruby-forum.com/t/testing-problems-acts-as-authenticated/48992/2 "2005-11-28T12:38:48Z")

</div>

Can you post up some of the actual errors you got? They will help  
track the issue down…

- james

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [November 28, 2005, 1:01pm UTC](https://www.ruby-forum.com/t/testing-problems-acts-as-authenticated/48992/3 "2005-11-28T13:01:54Z")

</div>

Okay … here goes … I know that the redirects are happening because  
of my changes to application.rb , but I don’t know how to modify the  
test or test environment to replicate a logged on user.

* * *

D:\svn\DontBinIt\Application\>ruby  
test\functional\users\_controller\_test.rb  
D:/svn/DontBinIt/Application/config/…/app/models/item.rb:38: warning:  
found = in conditional, should be ==  
Loaded suite test/functional/users\_controller\_test  
Started  
FFFFFFFF  
Finished in 0.172 seconds.

1. Failure:  
test\_create(UsersControllerTest)  
[test/functional/users\_controller\_test.rb:70]:  
response is not a redirection to all of the options supplied  
(redirection is \<{:  
controller=\>"/authenticate", :action=\>“login”}\>)

2. Failure:  
test\_destroy(UsersControllerTest)  
[test/functional/users\_controller\_test.rb:96]:

response is not a redirection to all of the options supplied  
(redirection is \<{:  
controller=\>"/authenticate", :action=\>“login”}\>)

1. Failure:  
test\_edit(UsersControllerTest)  
[test/functional/users\_controller\_test.rb:78]:  
Expected response to be a \<:success\>, but was \<302\>

2. Failure:  
test\_index(UsersControllerTest)  
[test/functional/users\_controller\_test.rb:32]:  
Expected response to be a \<:success\>, but was \<302\>

3. Failure:  
test\_list(UsersControllerTest)  
[test/functional/users\_controller\_test.rb:39]:  
Expected response to be a \<:success\>, but was \<302\>

4. Failure:  
test\_new(UsersControllerTest)  
[test/functional/users\_controller\_test.rb:58]:  
Expected response to be a \<:success\>, but was \<302\>

5. Failure:  
test\_show(UsersControllerTest)  
[test/functional/users\_controller\_test.rb:48]:  
Expected response to be a \<:success\>, but was \<302\>

6. Failure:  
test\_update(UsersControllerTest)  
[test/functional/users\_controller\_test.rb:88]:  
response is not a redirection to all of the options supplied  
(redirection is \<{:  
controller=\>"/authenticate", :action=\>“login”}\>)

8 tests, 15 assertions, 8 failures, 0 errors

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [November 28, 2005, 1:47pm UTC](https://www.ruby-forum.com/t/testing-problems-acts-as-authenticated/48992/4 "2005-11-28T13:47:53Z")

</div>

On 11/28/05, rails nut [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And here are the mods to application.rb …
> 
> include AuthenticatedSystem  
> before\_filter :login\_required  
> before\_filter :get\_login\_if\_any
> 
> def get\_login\_if\_any  
> current\_user  
> end

By putting those lines in your controller, all your actions will  
require a login for the tests. The generator generates a test helper  
for this purpose in lib/authenticated\_test\_helper.rb:

[http://techno-weenie.net/svn/projects/plugins/acts\_as\_authenticated/generators/authenticated/templates/authenticated\_test\_helper.rb](http://techno-weenie.net/svn/projects/plugins/acts_as_authenticated/generators/authenticated/templates/authenticated_test_helper.rb)

Be sure to include the helper in test/test\_helper.rb

class Test::Unit::TestCase  
include AuthenticatedTestHelper

self.use\_transactional\_fixtures = true  
self.use\_instantiated\_fixtures = false

Now in your test:

login\_as :bob # loads users(:bob) into the session

I apologize, I thought for sure I had better docs with this thing.  
I’ll be sure to clarify this part. Do let me know if there are other  
issues I missed 🙂

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [November 28, 2005, 1:08pm UTC](https://www.ruby-forum.com/t/testing-problems-acts-as-authenticated/48992/5 "2005-11-28T13:08:43Z")

</div>

And here are the mods to application.rb …

include AuthenticatedSystem  
before\_filter :login\_required  
before\_filter :get\_login\_if\_any

def get\_login\_if\_any  
current\_user  
end

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [November 29, 2005, 12:01pm UTC](https://www.ruby-forum.com/t/testing-problems-acts-as-authenticated/48992/6 "2005-11-29T12:01:55Z")

</div>

technoweenie wrote:

> By putting those lines in your controller, …

Thanks for that… looks like just what I need.

Busy with something else right now, but I’ll be giving that a shot soon.

Cheers, Andy

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [December 1, 2005, 7:16pm UTC](https://www.ruby-forum.com/t/testing-problems-acts-as-authenticated/48992/7 "2005-12-01T19:16:48Z")

</div>

> technoweenie wrote:
> 
> > By putting those lines in your controller, …

Yep, that DOES seem to have solved most of the problems. One or two  
remain, though. Theres this one from the functional tests (with  
associated code) …

* * *

1. Failure:  
test\_create(UsersControllerTest)  
[test/functional/users\_controller\_test.rb:101]:

Expected response to be a \<:redirect\>, but was \<200\>

* * *

def test\_create  
login\_as(:quentin)  
num\_users = User.count

```
post :create, :user => {}

assert_response :redirect
assert_redirected_to :action => 'list'

assert_equal num_users + 1, User.count

```

end

* * *

I’m wondering if the test is correct in this case … it looks as though  
the test is creating an ‘null’ user (no name etc) and (I guess) the  
create action is rendering the form with an error message.

Any thoughts ?

Andy

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [December 1, 2005, 11:07pm UTC](https://www.ruby-forum.com/t/testing-problems-acts-as-authenticated/48992/8 "2005-12-01T23:07:50Z")

</div>

> end  
> Yup, I copied that from the scaffolding generator. Obviously the  
> scaffolding generator has no clue how your models work. Perhaps I  
> could the required login feilds by default though for  
> acts\_as\_authenticated. Good catch. I’m used to filling those fields  
> manually 🙂

In your case, just make the tweak yourself. No sense on waiting for  
me to fix it so you can overwrite any changes you made.

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)
