# Functional tests - setup and teardown

**URL:** <https://www.ruby-forum.com/t/functional-tests-setup-and-teardown/191781>\
**Category:** Rails\
**Created:** [July 18, 2010, 3:40pm UTC](https://www.ruby-forum.com/t/functional-tests-setup-and-teardown/191781 "2010-07-18T15:40:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 18, 2010, 3:40pm UTC](https://www.ruby-forum.com/t/functional-tests-setup-and-teardown/191781/1 "2010-07-18T15:40:29Z")

</div>

Hi all,

functional test help please,

[http://pastie.org/1049300](http://pastie.org/1049300)

lines 5 to 8 simulate a login; all fine, but after line 56 I’d like to  
test the reverse that everything is blocked when the user is not logged  
in. So the question is how could I not do that setup routine after line  
56, or is there a better way ?

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 18, 2010, 7:06pm UTC](https://www.ruby-forum.com/t/functional-tests-setup-and-teardown/191781/2 "2010-07-18T19:06:40Z")

</div>

On Jul 18, 2:40 pm, bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> functional test help please,
> 
> [http://pastie.org/1049300](http://pastie.org/1049300)
> 
> lines 5 to 8 simulate a login; all fine, but after line 56 I’d like to  
> test the reverse that everything is blocked when the user is not logged  
> in. So the question is how could I not do that setup routine after line  
> 56, or is there a better way ?

You could just have two separate test files, or you might want to look  
at shoulda

Fred

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 18, 2010, 8:28pm UTC](https://www.ruby-forum.com/t/functional-tests-setup-and-teardown/191781/3 "2010-07-18T20:28:58Z")

</div>

Thanks Fred.

I actually did it like this in the end, I put this in test\_helper

# Add more helper methods to be used by all tests here…

def login  
@request.env[‘HTTP\_AUTHORIZATION’] = 'Basic ’ +  
Base64::encode64(“foo:bar”)  
end

and then in the tests like this

test “should destroy item” do  
login  
assert\_difference(‘Item.count’, -1) do  
delete :destroy, :id =\> items(:one).to\_param  
end

```
assert_redirected_to items_path

```

end

##########################

test “should not get index” do  
# don’t login in this test.  
get :index  
assert\_response :unauthorized  
assert\_nil assigns(:items)  
end

* * *

Would appreciate any comments on this approach.

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 19, 2010, 11:38am UTC](https://www.ruby-forum.com/t/functional-tests-setup-and-teardown/191781/4 "2010-07-19T11:38:55Z")

</div>

Is my approach a reasonable way to go? I only ask as it’s a problem I’ve  
come up against a few times (how to login in tests) - whether it’s via  
authlogic or basic http auth as in this case. Realising that I can add  
other methods to test\_helper.rb that could help me out in a similar way!

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 19, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/functional-tests-setup-and-teardown/191781/5 "2010-07-19T18:03:49Z")

</div>

bingo bob wrote:

> Hi all,
> 
> functional test help please,
> 
> [http://pastie.org/1049300](http://pastie.org/1049300)
> 
> lines 5 to 8 simulate a login; all fine, but after line 56 I’d like to  
> test the reverse that everything is blocked when the user is not logged  
> in. So the question is how could I not do that setup routine after line  
> 56, or is there a better way ?

There is a better way: use Cucumber. Functional tests basically suck.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 19, 2010, 6:15pm UTC](https://www.ruby-forum.com/t/functional-tests-setup-and-teardown/191781/6 "2010-07-19T18:15:56Z")

</div>

why?

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [July 19, 2010, 6:22pm UTC](https://www.ruby-forum.com/t/functional-tests-setup-and-teardown/191781/7 "2010-07-19T18:22:34Z")

</div>

bingo bob wrote:

> why?

Why what? Please quote when replying so we know what you’re replying  
to.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
