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 6877243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:34:37+00:00 2026-05-27T04:34:37+00:00

I am testing a model in CakePHP 2 and have mocked a model like

  • 0

I am testing a model in CakePHP 2 and have mocked a model like this:

$this->User = $this->getMock('User', array(
  '_saveUploadedFile',
  '_removeUploadedFile',
)); 
$this->User->expects($this->any())
        ->method('_saveUploadedFile')
        ->with($data, Configure::read('App.myDirectory'), true);
        ->will($this->returnValue(true));
$this->User->expects($this->any())
        ->method('_removeUploadedFile')
        ->with($data, Configure::read('App.myDirectory'))
        ->will($this->returnValue(true));

Since any operation with the database raises the following error:

"Database table mock__user_b6241a4cs for model User was not found."

I redefined the model information:

$this->User->alias = 'User';
$this->User->useTable = 'users';

Now the test works well, but it’s using the $default database in database.php instead of $test. What can be happening?

Why the database configuration for testing changes when using mocked objects? Could it be related to database permissions that causes the mocked object not being able to create its custom tables?

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-27T04:34:38+00:00Added an answer on May 27, 2026 at 4:34 am

    I finally solved the problem by passing the correct parameters to the model constructor in getMock() third agument:

    $this->User = $this->getMock('User',
      array('_saveUploadedFile', '_removeUploadedFile'),
      array(false, 'users', 'test')
    );
    

    Applying what is said in Stubs section of PHPUnit documentation, this third argument indicates that I want to use the users table and the test datasource.

    I have to keep the redefinition of the alias property though:

    $this->User->alias = 'User';
    

    because a simple $this->User->read(null, 1) raises an error saying that 'User.a_column' couldn’t be found.

    Thanks to José Lorenzo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my CakePHP application, I have a model like this: class Duck extends AppModel
I have this in my controller public ActionResult Testing() { CustomerContactModel model = new
Let's say in my user model I have a ChangePassword method. Given an already
So I'm having trouble with unit testing CakePHP models. Simple stuff like writing tests
I'm testing a model with an after create callback that I'd like to run
I have been testing inline function calls in C++. Thread model: win32 gcc version
I'm new to testing, so please forgive me for this question In the cakephp
I have a user model class User < ActiveRecord::Base has_many :languages, :dependent => :destroy
I'm testing a Grails model with MongoDB (mongodb plugin 1.0-M4) I have a Category
I have the following code as a middleware in an pylons application: import testing.model

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.