3

I'm doing a school project and we have to submit some unit testing examples . I read many tutorials but having trouble understanding how to use it .

This is my code below ,

I'm trying to check my validation rules by manual input :

<?php



class Student {

    public static $rules = array(

    'username'=>'required|alpha_num|min:6',

    'firstname'=>'required|alpha|min:2',

    'lastname'=>'required|alpha|min:2',

    'studentid'=>'numeric|min:7|unique:student',

    'email'=>'required|email|unique:student',

    'password'=>'required|alpha_num|between:6,12|confirmed',

    'password_confirmation'=>'required|alpha_num|between:6,12'

    );



}

class ChronosTest extends TestCase {

    public function testUser()
    {

        $student = new Student;

        //$student->username = "john433";



        $validator = Validator::make(array("username"=>"john433", "firstname"=>"johne"), Student::$rules);

        $this->assertTrue($validator->passes());



    }

}

I get this everytime i use assertTrue :

att
(source: 4.ii.gl)

I get this everytime i use assertFalse :

att
(source: 1.ii.gl)

2 Answers 2

5

Found the issue - It was returning false everytime as i did not include all the required inputs such as passwords etc..

The following works properly :

$validator = Validator::make(array(
    "username"=>"john433",
    "firstname"=>"john",
    "lastname"=>"doe",
    "email"=>"[email protected]",
    "password"=>"passtest",
    "password_confirmation"=>"passtest"
), Student::$rules);
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, Just came across this. May want to mark this as the "Answer" so that is shows up as such under searches. Happy coding!
0

What you are trying to test is Laravel code. One simply does not want to test code that he/her didn't write him/herself. Try finding a new example. Also; static attributes are very hard to test because they cannot be mocked. I guess you still have a lot to learn. Here's a good write-up on how to write tests and what to test: https://medium.com/laravel-4/48414f4782d0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.