Forum: Ruby on Rails tests and login_engine

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.
F719c6cb34bd5b691c12b373b7244b4d?d=identicon&s=25 Dorian Mcfarland (tachekent)
on 2006-04-28 06:30
(Received via mailing list)
Hi there,
I just started writing tests today after some months of using rails
(slap wrists).
I seem to be getting the hang of things, but I'm hitting a wall when it
comes to functional_testing pages protected by login_engine/user_engine.

all my assertions come back with:
Expected response to be a <:success>, but was <302>

Is there a way I can login from within a test?

thanks

dorian

--
I do things for love or money
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-04-28 11:03
(Received via mailing list)
The simplest way to 'login' when you are testing a protected
controller is to put a user in the session as part of your setup
method, i.e.

  class PithyCartoonTest < Test::Unit::TestCase

    def setup
      @controller = SuperRobotMonkeyTeamHyperForceGoController.new
      @request = ActionController::TestRequest.new
      @response = ActionController::TestResponse.new

      session[:user] = users(:chiro)
    end

    def test_should_be_able_to_summon_the_power_primate
      get 'team_member', :name => 'antauri'
      assert_response :success
     # and so on....
  end

Another good idea is defining a login method in your test_helper.rb
which does a similar thing.

HTH

- james


On 4/28/06, Dorian Mcfarland <loaf@isness.org> wrote:
>
> dorian
>
> --
> I do things for love or money
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
* J *
  ~
2fd982df89c3c697c487514796393bc5?d=identicon&s=25 Casper Fabricius (Guest)
on 2006-04-28 11:12
(Received via mailing list)
Dorian Mcfarland <loaf@...> writes:

>
> Hi there,
> I just started writing tests today after some months of using rails
 (slap wrists).
> I seem to be getting the hang of things, but I'm hitting a wall
 when it comes
to functional_testing pages
>
> --
> I do things for love or money
>

Hi Dorian,

What I did (not staying entirely DRY) was to copy all of the *.yml-files
from
/vendor/plugins/user_engine/test/fixtures to /test/fixtures and then add
fixtures :users, :roles, :users_roles (and other fixtures you need) to
the test
cases that need authentication.

This ensures that user data is loaded for each test, and makes you able
to add:

  def setup
    ...
    @request.session[:user] = User.find(1)
  end

- to each test case, which will "authenticate" you as the administrator
(with
id=1) for all your tests.

Best regards,
Casper Fabricius
This topic is locked and can not be replied to.