Setup method in functional tests and instance variables


#1

I have the following in my functional test file.

class UserControllerTest < ActionController::TestCase
fixtures :users

def setup
@controller = UserController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@invalid_user = user(:invalid_user)
@valid_user = users(:valid_user)
end

def test_login_success
@valid_user.screen_name
end

When I run a test that tries to use the @valid_user variable I get the
following error.

NoMethodError: You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.screen_name

It seems that this class isn’t storing the instance variables in memory.

Any ideas.

Thanks in advance

Mitch


#2

On 6 Apr., 15:36, Mitchell G. removed_email_address@domain.invalid
wrote:

@valid_user = users(:valid_user)
The error occurred while evaluating nil.screen_name

It seems that this class isn’t storing the instance variables in memory.

Any ideas.

The above should work just fine, given that you have a user fixture
named ‘valid_user’. Do you see something like this in test/fixtures/
users.yaml?

valid_user:
screen_name: xyz


Best regards,
David K.
http://twitter.com/rubyguy


#3

David K. wrote:

On 6 Apr., 15:36, Mitchell G. removed_email_address@domain.invalid
wrote:

� @valid_user = users(:valid_user)
The error occurred while evaluating nil.screen_name

It seems that this class isn’t storing the instance variables in memory.

Any ideas.

The above should work just fine, given that you have a user fixture
named ‘valid_user’. Do you see something like this in test/fixtures/
users.yaml?

valid_user:
screen_name: xyz


Best regards,
David K.
http://twitter.com/rubyguy

HI David

This is my yaml

valid_user:
id: 1
screen_name: millikan
email: removed_email_address@domain.invalid
password: electron

Now here is the weird thing.

if I do the following in the actual test method

def test_login_success
@valid_user = users(:valid_user)
@valid_user.screen_name
end

it works just fine.

Its like I can’t access instance variables that run in the setup. Why
would that be?

For my units test I have a similar setup and it works no problem.


#4

Just found the answer

it’s worth noting that R2.0.2 subclasses ActionController::TestCase
for functional tests (i.e. UserControllerTest), not
ActiveSupport::TestCase. You need to explicitly change the skeleton
test files for (user|spec|etc)_controller_test.rb files to subclass
ActiveSupport::TestCase.

So I had to change the test file skeleton class definition to

ActiveSupport::TestCase

from

ActionController::TestCase

works like a charm.

Why don’t they fix this?

Mitch


#5

On 6 Apr., 17:18, Mitchell G. removed_email_address@domain.invalid
wrote:

ActiveSupport::TestCase

from

ActionController::TestCase

works like a charm.

Why don’t they fix this?

What do mean when saying “fix this”. Did you run the rails:update rake
task?


Best regards,
David K…
http://twitter.com/rubyguy


#6

David K. wrote:

On 6 Apr., 17:18, Mitchell G. removed_email_address@domain.invalid
wrote:

ActiveSupport::TestCase

from

ActionController::TestCase

works like a charm.

Why don’t they fix this?

What do mean when saying “fix this”. Did you run the rails:update rake
task?


Best regards,
David K…
http://twitter.com/rubyguy

I did run rails:update rake. And I updated rails. But even now when I
run script/generate controller someController I have to edit the test
file so that it is changed from ActionController::Testcase to
ActiveSupport::TestCase.

Should I update something else so I don’t have to do this everytime? And
it does this only for the functionals not the unit test skeletons.

Mitch


#7

David K. wrote:

On 8 Apr., 04:04, Mitchell G. removed_email_address@domain.invalid
wrote:

I did run rails:update rake. �And I updated rails. �But even now when I
run script/generate controller someController I have to edit the test
file so that it is changed from ActionController::Testcase to
ActiveSupport::TestCase.

Should I update something else so I don’t have to do this everytime? And
it does this only for the functionals not the unit test skeletons.

Did you upgrade to another version of Rails recently?


Best regards,
David K.
http://twitter.com/rubyguy

I am using rails 2.0.2. I have written the app with this. If I move up
to another version my application does not work.

What do you suggest I do? How can I move my app to say 2.3.2 and have it
still work?

Thanks


#8

On 8 Apr., 04:04, Mitchell G. removed_email_address@domain.invalid
wrote:

I did run rails:update rake. And I updated rails. But even now when I
run script/generate controller someController I have to edit the test
file so that it is changed from ActionController::Testcase to
ActiveSupport::TestCase.

Should I update something else so I don’t have to do this everytime? And
it does this only for the functionals not the unit test skeletons.

Did you upgrade to another version of Rails recently?


Best regards,
David K.
http://twitter.com/rubyguy


#9

Milan Iliev wrote:

Mitchell,

Sounds like you should maybe just bite the bullet and upgrade to 2.3.2
if that’s being an issue.

When you say “If I move up to another version [of Rails], my
application does not work”, what do you mean? Can you provide specific
errors?

Best,
Milan

On Apr 22, 12:48�am, Mitchell G. removed_email_address@domain.invalid

Just a quick reply.

Two things:

  1. How do I move my app to 2.3.2? Do I just change the configuration
    settings?
  2. When I have done this in the past the application won’t start up
    meaning the server won’t launch.

Thanks for your reply

Mitch


#10

Mitchell,

Sounds like you should maybe just bite the bullet and upgrade to 2.3.2
if that’s being an issue.

When you say “If I move up to another version [of Rails], my
application does not work”, what do you mean? Can you provide specific
errors?

Best,
Milan

On Apr 22, 12:48 am, Mitchell G. removed_email_address@domain.invalid