# Running tests

**URL:** <https://www.ruby-forum.com/t/running-tests/146875>\
**Category:** Rails\
**Created:** [August 26, 2008, 11:54am UTC](https://www.ruby-forum.com/t/running-tests/146875 "2008-08-26T11:54:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertpostill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@robertpostill](https://www.ruby-forum.com/u/robertpostill)\
**Post date:** [August 26, 2008, 11:54am UTC](https://www.ruby-forum.com/t/running-tests/146875/1 "2008-08-26T11:54:14Z")

</div>

Hi,  
In a controler, how can we check that we are running a test (rake test).  
Is there a connection object?

---

<div class="post-metadata">

**Author:** ![robertpostill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@robertpostill](https://www.ruby-forum.com/u/robertpostill)\
**Post date:** [August 26, 2008, 12:07pm UTC](https://www.ruby-forum.com/t/running-tests/146875/2 "2008-08-26T12:07:33Z")

</div>

On 26 Aug 2008, at 10:54, Robert Plagnard wrote:

> Hi,  
> In a controler, how can we check that we are running a test (rake  
> test).  
> Is there a connection object?

Check the value of RAILS\_ENV

Fred

---

<div class="post-metadata">

**Author:** ![robertpostill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@robertpostill](https://www.ruby-forum.com/u/robertpostill)\
**Post date:** [August 26, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/running-tests/146875/3 "2008-08-26T12:20:14Z")

</div>

Frederick C. wrote:

> On 26 Aug 2008, at 10:54, Robert Plagnard wrote:
> 
> > Hi,  
> > In a controler, how can we check that we are running a test (rake  
> > test).  
> > Is there a connection object?
> 
> Check the value of RAILS\_ENV
> 
> Fred

Super,  
Thanks

Robert

---

<div class="post-metadata">

**Author:** ![robertpostill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@robertpostill](https://www.ruby-forum.com/u/robertpostill)\
**Post date:** [August 26, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/running-tests/146875/4 "2008-08-26T16:12:32Z")

</div>

On Tue, Aug 26, 2008 at 2:54 AM, Robert Plagnard  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> In a controler, how can we check that we are running a test (rake test).  
> Is there a connection object?

I can’t help but ask… why?

Curious,  
Robby

–  
Robby R.  
Chief Evangelist, Partner

PLANET ARGON, LLC  
design // development // hosting

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
aim: planetargon

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]

---

<div class="post-metadata">

**Author:** ![robertpostill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@robertpostill](https://www.ruby-forum.com/u/robertpostill)\
**Post date:** [August 26, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/running-tests/146875/5 "2008-08-26T17:14:37Z")

</div>

Robby R. wrote:

> On Tue, Aug 26, 2008 at 2:54 AM, Robert Plagnard  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi,  
> > In a controler, how can we check that we are running a test (rake test).  
> > Is there a connection object?
> 
> I can’t help but ask… why?
> 
> Curious,  
> Robby
> 
> –  
> Robby R.  
> Chief Evangelist, Partner
> 
> PLANET ARGON, LLC  
> design // development // hosting
> 
> [http://www.planetargon.com/](http://www.planetargon.com/)  
> [http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
> aim: planetargon

Hi,

I am studying Noel Rappin’s “Professional Ruby on Rails” excellent book.  
I met the problem of update user.is\_active attribute p 83.  
In users\_controller :

def activate  
find\_user  
@is\_valid = Token.is\_valid\_for\_user(@user, param[:token])  
@user.update\_attributes(:is\_active=\>@is\_valid)  
end

Everything was fine except that user.is\_active was staying desperatly  
false!  
After replacing update\_attributes by update\_attributes! I found the  
reason why. The user record was not valid because of the constraint :  
validates\_length\_of :password, :minimum =\> 6  
But the password is not stored in user, we only have encrypted\_password.  
It will not be a problem in production because we will have the password  
at this moment.  
But during the test, it’s not the case. And then you have 2 solutions :  
One used by NR which is to comment the constraint to pass the test. I  
don’t like it to much. The other idea was, when in test, to set a  
password just before update the user record :  
if RAILS\_ENV = ‘test’  
@user.password= ‘123456’  
end  
@user.update\_attributes(:is\_active=\>@is\_valid)

I don’t know if I was understandable, but it works as I want.

Robert

---

<div class="post-metadata">

**Author:** ![robertpostill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@robertpostill](https://www.ruby-forum.com/u/robertpostill)\
**Post date:** [August 26, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/running-tests/146875/6 "2008-08-26T20:30:28Z")

</div>

Robert Plagnard wrote:

> > > In a controler, how can we check that we are running a test (rake  
> > > test).

> > > Is there a connection object?

> > Check the value of RAILS\_ENV

Now confess why your code needs to know that. You should either write  
the code  
to work no matter what its environment, or you should use a mock.

If you want to use Mocha, use any\_instance if you don’t know which  
instance your  
controller will construct…

–  
Phlip
