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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:08:20+00:00 2026-06-02T09:08:20+00:00

I am having troubles to test one of my methods because it uses a

  • 0

I am having troubles to test one of my methods because it uses a vendor class with no model.

Well, the thing is that i want that method to return me what i want.
I have been told that mocking a method is to make it return what i want.

For example, when i call “foo()” method i want it to return me true always.

How can i do it? At CakePHP cookbook i can find this:

$Posts = $this->generate('Posts', array(
    'methods' => array(
        'isAuthorized'
    ),
    'models' => array(
        'Post' => array('save')
    ),
    'components' => array(
        'RequestHandler' => array('isPut'),
        'Email' => array('send'),
        'Session'
    )
));

So i guess i should have to use the fist option: method
But… how to make it return what i want?

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-06-02T09:08:22+00:00Added an answer on June 2, 2026 at 9:08 am

    Refer to the answer I gave you in this question: How can i test an Add function on CakePHP2.0

    $Posts = $this->generate('Posts', array(
        'methods' => array(
            'isAuthorized'
        ),
        'models' => array(
            'Post' => array('save')
        ),
        'components' => array(
            'RequestHandler' => array('isPut'),
            'Email' => array('send'),
            'Session'
        )
    ));
    
    // tell PHPUnit that `isAuthorized` should return true any time it's called
    $Posts
      ->expects($this->any())
      ->method('isAuthorized')
      ->will($this->returnValue(true));
    // tell PHPUnit to expect `isPut` once, and to return false
    $Posts
      ->RequestHandler
      ->expects($this->once())
      ->method('isPut')
      ->will($this->returnValue(false));
    

    For more information on mocking: http://www.phpunit.de/manual/3.0/en/mock-objects.html

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

Sidebar

Related Questions

I am having some trouble with writing a unit test that checks my custom
im having troubles with adding a class to the last option from foreach, its
I'm having troubles finding a bad query that's really causing high CPU load due
I'm having trouble deserializing a ruby class that I wrote to YAML. Where I
I'm having troubles reading a Json result back from a controller method... I have
I'm having trouble writing a unit test for one of my controller actions. Here's
I am having trouble figuring one of several error messages that seem to have
I am having troubles while trying to post some datas with one file (an
I am having trouble including a test under a cmake project. My project is
I am currently having unnecessary trouble trying to test out RubyMine. My biggest problem

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.