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

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决