Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6848173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:50:08+00:00 2026-05-27T00:50:08+00:00

I am trying to test a controller action that allows edition of user profiles.

  • 0

I am trying to test a controller action that allows edition of user profiles. Among other things I want to test that every logged user can only edit its own profile and not other’s. In case of breaking this restriction the action must redirect to a predefined home page.

With this scenario, I have a fixture that creates a user with ID = 1. So I was thinking on testing the restriction this way:

$data = $this->Users->User->read(null, 1); 
$this->Users->Auth->login($data); 
$this->testAction('/users/edit/2', array('method' => 'get')); 
$url = parse_url($this->headers['Location']); 
$this->assertEquals($url['path'], '/homepage'); 

The test passes this assert. So the next step is to check if executing '/users/edit/1', which has the ID of the logged user, shows the form:

$this->testAction('/users/edit/1', array('method' => 'get', 'return' => 'vars'));
$matcher = array( 
  'tag' => 'form', 
  'ancestor' => array('tag' => 'div'), 
  'descendant' => array('tag' => 'fieldset'), 
); 
$this->assertTag($matcher, $this->vars['content_for_layout'], 'The edition form was not found');

However this assert fails. After digging around with debug() I’ve found that $this->Auth->user() returns the whole information but $this->Auth->user('id') returns null. Since I use the latter in a comparison within the action, it evaluates as false and causes the
test to fail.

The curious thing is that it happens when testing but not when executing the action in a browser. So, what’s the correct way of testing this action?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:50:09+00:00Added an answer on May 27, 2026 at 12:50 am

    The actual correct answer should be using mock objects instead of actually login the user in manually:

    $this->controller = $this->generate('Users', array(
        'components' => array('Auth' => array('user')) //We mock the Auth Component here
    ));
    $this->controller->Auth->staticExpects($this->once())->method('user') //The method user()
        ->with('id') //Will be called with first param 'id'
        ->will($this->returnValue(2)) //And will return something for me
    $this->testAction('/users/edit/2', array('method' => 'get')); 
    

    Using mocks is the most easy way to test a controller, and also the most flexible one

    Update 11 March 2015

    You can also mock all method of AuthComponent

    $this->controller = $this->generate('Users', array(
        'components' => array('Auth') // Mock all Auth methods
    ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to unit test a controller action that uses UpdateModel but I
I am running on Castle's trunk, and trying to unit-test a controller-action where validation
I am trying to test a Controller that has a Command object with data
I'm trying to test a rails action that takes raw json in the POST
I am trying to test my controller's create action. I am using Devise for
Below is my action in the controller. I am trying to test this action
I'm trying to test a method on the application controller that will be used
I'm trying to write a test for an ASP.Net MVC controller action. I'd like
I'm new to rails and I'm trying to test a controller with rspec. My
Trying to figure out how to adequately test my accounts controller. I am having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.