3

I am tring to make test for my project, could someone show me example how to write tests or give me some good video course to learn testing.

In my example I am tring to test this part:

public function getProjectsForUser(){
        if(Auth::user() -> admin_id == NULL){
            $this->id = Auth::user() -> id;
        }else{
            $this->id = Auth::user() -> admin_id;
        }
        $projects = User::findOrFail(Auth::user() -> id)->projects()->where('admin_id', '=', $this->id)->get();

        $role = User::findOrFail(Auth::user() -> id)->roles;

        $users = User::where('admin_id', '=', $this->id)->lists('name','id');

        return array('projects' => $projects,'users' => $users,'roles' => $role );
}

It was my model Project.php

Here is my PorojectController:

public function project(Project $projects){
            $this -> projects = $projects ->getProjectsForUser();
            return view('projects',$this -> projects);
        }

Here is my test to check if user logged...

public function testHome()
        {
            $this->be(User::find(1));
            $response = $this->call('GET', '/projects');
            //CHECK IF AUTH USER
            $this->assertTrue($response->isOk());

    }

Now I need to check if I get right values inside array, for $project, $role, $users.

1 Answer 1

1

You can use assertViewHas() to assert values that should have been passed to the view. For example:

public function testHome(){
    $this->be(User::find(1));
    $response = $this->call('GET', '/projects');
    //CHECK IF AUTH USER
    $this->assertTrue($response->isOk());

    $this->assertViewHas('projects', 'expected value for projects');
    $this->assertViewHas('users', 'expected value for users');
    $this->assertViewHas('roles', 'expected value for roles');
}

By the way: If you pass no second argument the method will just assert that the variable exists but won't compare the value.

To accomplish the same with a bit a different syntax you can use assertViewHasAll() and pass everything as array:

$this->assertViewHasAll([
    'projects' => 'expected value for projects',
    'users' => 'expected value for users',
    'roles' => 'expected value for roles'
]);
Sign up to request clarification or add additional context in comments.

4 Comments

How can I check it dinamicly if user is changed?
If user is admin I need to check if form exists, if user is worker I need to check for string happy working and if user not auth then check if redirected to /
You can assert a redirect with assertRedirectTo('/'). I don't think checking the actual content of the view is something you should be doing although you can access the html with $response->getOriginalConten()
@lukasgeiter sorry maybe you can help to get rid of my assertViewHasAll problem stackoverflow.com/questions/40138855/…

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.