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

  • Home
  • SEARCH
  • 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 7953843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:15:06+00:00 2026-06-04T03:15:06+00:00

New to unit testing… testing an articles controller and I am getting a fail

  • 0

New to unit testing… testing an articles controller and I am getting a fail on the $this->assertNotEmpty();

Shouldn’t this be displaying an array full of validation errors? Instead I am getting an empty array.

It seems my validation rules are not being picked up… as further inspection show that Article::save() is returning true on data that should fail….

/**
     * Admin Add
     * @see controllers/MastersController::_admin_add()
     * @return void
     */
    public function admin_add(){
        //parent::_admin_add();
            if(!empty($this->request->data){
            $this->Article->save($this->request->data);
            }
    }

/**
     * Test Admin Add
     *
     * @return void
     */
    public function testAdminAdd() {
        #define sample passing data     
        $sampleDataPass = array(
            'Article'=>array(
                'title'=>'Test Article Add Will Pass',
                'body'=>'Test Article Add Body',
                'status_id'=>1,
                'category_id'=>1,
            )
        );

        #test action        
        $this->testAction('admin/articles/add', array('data'=>$sampleDataPass));
        $this->assertEmpty($this->Articles->Article->validationErrors); #####PASSES#####

        #define sample failing data     
        $sampleDataFail = array(
            'Article'=>array(
                'title'=>'Test Article Add Will Fail',
            )
        );



        $this->testAction('admin/articles/add', array('data'=>$sampleDataFail));
        $this->assertNotEmpty($this->Articles->Article->validationErrors); #####FAILS#####  
    }

class Article extends AppModel {

    /*
     * Name
     */
    public $name = 'Article';

    /*
     * Validation Rules
     */
    public $validate = array(
        'title' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'You must supply an article title in order to save.',
            ),
        ),
        'body' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'You must supply an article title in order to save.',
            ),
        ),
        'status_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'You must choose a status.',
                'allowEmpty' => false,
            ),
        ),
        'category_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'You must choose a category.',
                'allowEmpty' => false,
            ),
        )
    );
}
  • 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-04T03:15:08+00:00Added an answer on June 4, 2026 at 3:15 am

    CakePHP will ignore validation rules if the field is not present in the data.

    By setting the option ‘required’ to true the validation rule will always be checked.

    For example:

       'title' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'You must supply an article title in order to save.',
                'required' => true
            ),
        ),
    

    Documention on validation in CakePHP can be found here: http://book.cakephp.org/2.0/en/models/data-validation.html#one-rule-per-field

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

Sidebar

Related Questions

I'm new to Unit Testing, and I'm only getting into the routine of building
I am new to unit testing. I have done unit testing on controller classes
I'm just getting going with ASP.NET MVC and I'm also new to unit testing
Disclaimer: I'm new to unit testing and just getting my feet wet. That being
I am new to unit testing, just getting into it using Check for C.
I am new to unit testing/TDD and having problem getting my head certain aspects
I am new to unit testing, and would like to know how you would
I'm new to unit testing and NUit in particular. I'm just typing some examples
I am new to Unit Testing and think I might have dug myself into
I'm still new to Unit testing, and specifically PHPUnit as the testing framework. Suppose

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.