Gammer
Gammer

Reputation: 5608

Laravel PHPunit test: 'testUser' does not match expected type "object"

I have the following test :

class AdminPanelTest extends TestCase
{
    public function photoUpload()
    {
        $user = new App\User;
        $user->username = "testUser";
        $user->email = "[email protected]";
        $user->password = bcrypt("testUser");
        $user->photo_url = "abc.jpg";
        $user->save();

        $test = App\User::where('username','=','testUser');
        $this->assertEquals($test,'testUser');
        $this->assertCount(1,$test);
    }
}

The result says that 'testUser' does not match expected type "object"..

The user is added to the database.

Am I missing something ?

Upvotes: 0

Views: 2180

Answers (1)

Jeff Puckett
Jeff Puckett

Reputation: 40861

Rename your function to begin with "test"

public function testPhotoUpload()

App\User::where('username','=','testUser') is returning an Object, but you're expecting it to return just a username string 'testUser' so it's failing your tests.

I'm unfamiliar with your App\User class, but just guessing from convention, it's probably going to have a property named something like username

In that case, then I would expect your test to pass if you compared those values instead, i.e. the expected string literal 'testUser' and the object property $test->username

Also, assertEquals by convention wants you to put your expected value as the first parameter, and the actual value as the second parameter.

$this->assertEquals('testUser', $test->username);

Upvotes: 1

Related Questions