douyi1944 2016-06-26 17:48
浏览 25
已采纳

Laravel PHPunit测试:'testUser'与预期类型“对象”不匹配

I have the following test :

class AdminPanelTest extends TestCase
{
    public function photoUpload()
    {
        $user = new App\User;
        $user->username = "testUser";
        $user->email = "testUser@hotmail.com";
        $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 ?

  • 写回答

1条回答 默认 最新

  • dshgnt2008 2016-06-26 17:52
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?