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.
Mitchell G. (Guest)
on 2009-04-06 17: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
David K. (Guest)
on 2009-04-06 18:07
(Received via mailing list)
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
Mitchell G. (Guest)
on 2009-04-06 18:59
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.
Mitchell G. (Guest)
on 2009-04-06 19: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
David K. (Guest)
on 2009-04-07 19:37
(Received via mailing list)
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
Mitchell G. (Guest)
on 2009-04-08 06:04
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
David K. (Guest)
on 2009-04-11 13:25
(Received via mailing list)
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
Mitchell G. (Guest)
on 2009-04-22 08:48
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
Milan Iliev (Guest)
on 2009-05-06 21: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 G. <removed_email_address@domain.invalid>
Mitchell G. (Guest)
on 2009-05-15 13: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 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
This topic is locked and can not be replied to.