# Method works but tests fail?

**URL:** <https://www.ruby-forum.com/t/method-works-but-tests-fail/120524>\
**Category:** Rails\
**Created:** [November 17, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/method-works-but-tests-fail/120524 "2007-11-17T22:54:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [November 17, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/method-works-but-tests-fail/120524/1 "2007-11-17T22:54:43Z")

</div>

Hello Rubyists,  
I’m a bit stumped on this one. I have a signup controller that lets  
people add their name, email address and a passcode to a mailing list.  
When I go through the UI, everything works as I would expect - a record  
is added to the appropriate table in the database, and the flash sends  
the greeting I want to the next page. But when I run my functional  
tests, they blow up! Not sure what to do at this point, so hopefully a  
kind soul on here can point me in the right direction.

Okay, the code follows below. There’s no code in the model other than  
the usual "validates’ methods, so I didn’t include it.

Error message:

1. Failure:  
test\_sign\_up\_adds\_new\_recipient(SignupControllerTest)  
[/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/assertions/response\_assertions.rb:26:in  
`assert_response' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:18:in`assert\_response’  
/home/kodama/blog/test/functional/signup\_controller\_test.rb:20:in  
`test\_sign\_up\_adds\_new\_recipient’]:  
Expected response to be a \<:redirect\>, but was \<200\>

My test method: (all the asserts fail)

def test\_sign\_up\_adds\_new\_recipient  
num\_recipients = Recipient.count  
post :signup, :signup =\> {:name =\> “test”, :email\_address =\>  
“removed\_email\_address@domain.invalid”,:passcode =\> “test”}  
assert\_response :redirect  
assert\_equal “Thank you for signing up!”, flash[:notice]  
assert\_equal num\_recipients + 1, Recipient.count  
end

My controller code: (standard controller, nothing special other than  
this method)

#Add a new recipient through the sign-up sheet  
def signup  
@recipient = Recipient.new(params[:signup])  
if @recipient.save  
flash[:notice] = “Thank you for signing up!”  
redirect\_to :controller =\> ‘read’, :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

My view code: (signup/new.rhtml)

\<% form\_tag :action =\> ‘signup’ do %\>  
\<%= render :partial =\> ‘signup’ %\>  
\<%= submit\_tag “Sign me up!” %\>  
\<% end %\>

The partial:

Thanks,  
kodama

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [November 18, 2007, 12:51am UTC](https://www.ruby-forum.com/t/method-works-but-tests-fail/120524/2 "2007-11-18T00:51:56Z")

</div>

There’s nothing I can see from the code from your example that is  
wrong. Obviously, the object is not saving so I suspect that the  
parameters you are passing are incorrect and failing validation. Use  
an assert\_valid to find out where the validation errors are.

[http://api.rubyonrails.org/classes/ActionController/Assertions/ModelAssertions.html](http://api.rubyonrails.org/classes/ActionController/Assertions/ModelAssertions.html)

e.g. assert\_valid(assigns(:recipient))

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [November 18, 2007, 3:20am UTC](https://www.ruby-forum.com/t/method-works-but-tests-fail/120524/3 "2007-11-18T03:20:21Z")

</div>

Nicholas H. wrote:

> There’s nothing I can see from the code from your example that is  
> wrong. Obviously, the object is not saving so I suspect that the  
> parameters you are passing are incorrect and failing validation. Use  
> an assert\_valid to find out where the validation errors are.
> 
> [http://api.rubyonrails.org/classes/ActionController/Assertions/ModelAssertions.html](http://api.rubyonrails.org/classes/ActionController/Assertions/ModelAssertions.html)
> 
> e.g. assert\_valid(assigns(:recipient))

Thank you so much! The issue was that I was violating uniqueness  
constraints…I think I left stale data in there, or I have to go  
double-check my fixtures. In either case, this fixed the problem:

#Add Time.now to name to guarantee uniqueness  
post :signup, :signup =\> {:name =\> “test#{Time.now}”, :email\_address =\>  
“removed\_email\_address@domain.invalid”,:passcode =\> “test”}

Thanks again! 🙂
