# Functional tsts

**URL:** <https://www.ruby-forum.com/t/functional-tsts/97305>\
**Category:** Rails\
**Created:** [July 6, 2006, 8:47am UTC](https://www.ruby-forum.com/t/functional-tsts/97305 "2006-07-06T08:47:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [July 6, 2006, 8:47am UTC](https://www.ruby-forum.com/t/functional-tsts/97305/1 "2006-07-06T08:47:13Z")

</div>

Hi all,

```
  I have created projects module in my application using scaffold, 

```

and it created me everything, and all the modules are working except the  
functional test. This is the error

1. Failure:  
test\_create(ProjectsControllerTest)  
[test/functional/projects\_controller\_test.rb  
:55]:  
Expected response to be a \<:redirect\>, but was \<200\>

8 tests, 25 assertions, 1 failures, 0 errors

50 def test\_create  
51 num\_projects = Project.count  
52  
53 post :create, :project =\> {}  
54  
55 assert\_response :redirect  
56 assert\_redirected\_to :action =\> ‘list’  
57  
58 assert\_equal num\_projects + 1, Project.count  
59 end

Thanks in Advance

-Harish

---

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [July 6, 2006, 11:39am UTC](https://www.ruby-forum.com/t/functional-tsts/97305/2 "2006-07-06T11:39:08Z")

</div>

Naga Harish K. wrote:

> Hi all,
> 
> ```
> I have created projects module in my application using scaffold, 
> 
> ```
> 
> and it created me everything, and all the modules are working except the  
> functional test. This is the error
> 
> 1. Failure:  
> test\_create(ProjectsControllerTest)  
> [test/functional/projects\_controller\_test.rb  
> :55]:  
> Expected response to be a \<:redirect\>, but was \<200\>
> 
> 8 tests, 25 assertions, 1 failures, 0 errors
> 
> 50 def test\_create  
> 51 num\_projects = Project.count  
> 52  
> 53 post :create, :project =\> {}  
> 54  
> 55 assert\_response :redirect  
> 56 assert\_redirected\_to :action =\> ‘list’  
> 57  
> 58 assert\_equal num\_projects + 1, Project.count  
> 59 end
> 
> Thanks in Advance
> 
> -Harish

Hi Harish. So, your create method in projects\_controller.rb is not  
redirecting like you expected it to.

Do you maybe have some validation in the project.rb model that is  
preventing the creation of a new project with no attributes?

If you post the relevant bit of projects\_controller.rb and you  
project.rb model it would make it easier to see what’s gone wrong.

Robert J.

---

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [July 6, 2006, 11:45am UTC](https://www.ruby-forum.com/t/functional-tsts/97305/3 "2006-07-06T11:45:10Z")

</div>

Surely Robert J.,

```
I'm sending you the piece of code.

```

project.rb::

class Project \< ActiveRecord::Base  
belongs\_to:user  
has\_many:pictures  
validates\_presence\_of :name,:with =\> //,:message =\> "is Invalid "  
validates\_presence\_of :description,:with =\> //,:message =\> "is Invalid  
"  
end

projects\_controller.rb::

def create  
@project = Project.new(params[:project])  
if @project.save  
flash[:notice] = ‘Project was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

~Harish

---

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [July 6, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/functional-tsts/97305/4 "2006-07-06T16:11:45Z")

</div>

The line in your test doesn’t give the values needed. I think you  
should have something like:

post :create, :project =\> { :name =\> ‘A name’, :description =\> ‘This  
is a description’ }

Instead of:

post :create, :project =\> {}

---

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [July 6, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/functional-tsts/97305/5 "2006-07-06T18:50:19Z")

</div>

On 7/6/06, Jordan E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The line in your test doesn’t give the values needed. I think you  
> should have something like:
> 
> post :create, :project =\> { :name =\> ‘A name’, :description =\> ‘This  
> is a description’ }
> 
> Instead of:
> 
> post :create, :project =\> {}

This is exactly correct, project.save is failing because of the  
validates\_presence\_of. But you’re on to a good point about testing,  
you should be doing negative and positive tests to make sure that when  
it fails it does exactly what you want it to. So keep the test you  
have and add a new test that checks valid input as well. Woot, two  
tests for the price of one!

Cheers,  
Chuck V.

---

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [July 7, 2006, 9:07am UTC](https://www.ruby-forum.com/t/functional-tsts/97305/6 "2006-07-07T09:07:16Z")

</div>

Chuck V. wrote:

> This is exactly correct, project.save is failing because of the  
> validates\_presence\_of.

Hi,

I removed the “validates\_presence\_of” then i got working good.

Thanku very much…

~Harish

---

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [July 7, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/functional-tsts/97305/7 "2006-07-07T21:53:22Z")

</div>

Well, don’t forget to put it back in. You see, your tests were  
actually working correctly - they were telling you that your code,  
the code being tested, is not functioning as you expected it to.  
Perhaps it would have been better to rename your test to indicate  
that you are expecting it to get a 200 response, and then write  
another test to check out whether or not the redirect is happening?

Hope that helps

* * *

Peter Wright  
removed\_email\_address@domain.invalid

Personal Blog → [http://peterwright.blogspot.com](http://peterwright.blogspot.com)  
Agile Development Blog → [http://exceeding-expectations.blogspot.com](http://exceeding-expectations.blogspot.com)
