Forum: Ruby on Rails setup method in functional tests and instance variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aab060b3da7fa08ba0b531e2204c6469?d=identicon&s=25 Mitchell Gould (mitch_newbie)
on 2009-04-06 15:36
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
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 David Knorr (Guest)
on 2009-04-06 16:07
(Received via mailing list)
On 6 Apr., 15:36, Mitchell Gould <rails-mailing-l...@andreas-s.net>
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 Knorr
http://twitter.com/rubyguy
Aab060b3da7fa08ba0b531e2204c6469?d=identicon&s=25 Mitchell Gould (mitch_newbie)
on 2009-04-06 16:59
David Knorr wrote:
> On 6 Apr., 15:36, Mitchell Gould <rails-mailing-l...@andreas-s.net>
> 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 Knorr
> http://twitter.com/rubyguy

HI David

This is my yaml

 valid_user:
   id: 1
   screen_name: millikan
   email: ram@example.com
   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.
Aab060b3da7fa08ba0b531e2204c6469?d=identicon&s=25 Mitchell Gould (mitch_newbie)
on 2009-04-06 17:18
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
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 David Knorr (Guest)
on 2009-04-07 17:37
(Received via mailing list)
On 6 Apr., 17:18, Mitchell Gould <rails-mailing-l...@andreas-s.net>
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 Knorr.
http://twitter.com/rubyguy
Aab060b3da7fa08ba0b531e2204c6469?d=identicon&s=25 Mitchell Gould (mitch_newbie)
on 2009-04-08 04:04
David Knorr wrote:
> On 6 Apr., 17:18, Mitchell Gould <rails-mailing-l...@andreas-s.net>
> 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 Knorr.
> 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
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 David Knorr (Guest)
on 2009-04-11 11:25
(Received via mailing list)
On 8 Apr., 04:04, Mitchell Gould <rails-mailing-l...@andreas-s.net>
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 Knorr
http://twitter.com/rubyguy
Aab060b3da7fa08ba0b531e2204c6469?d=identicon&s=25 Mitchell Gould (mitch_newbie)
on 2009-04-22 06:48
David Knorr wrote:
> On 8 Apr., 04:04, Mitchell Gould <rails-mailing-l...@andreas-s.net>
> 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 Knorr
> 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
6589c49345f4fec210c89b6a42d273ba?d=identicon&s=25 Milan Iliev (Guest)
on 2009-05-06 19:51
(Received via mailing list)
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 Gould <rails-mailing-l...@andreas-s.net>
Aab060b3da7fa08ba0b531e2204c6469?d=identicon&s=25 Mitchell Gould (mitch_newbie)
on 2009-05-15 11:20
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 Gould <rails-mailing-l...@andreas-s.net>

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
This topic is locked and can not be replied to.