Running tests

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

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

Frederick Cheung wrote:

I can't help but ask... why?

Curious,
Robby

Robby Russell 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 Russell
Chief Evangelist, Partner

PLANET ARGON, LLC
design // development // hosting

http://www.planetargon.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

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...