小编Gur*_*ura的帖子

在调用时,模拟security.context被视为非对象

我正在尝试测试我的服务.此服务调用其他服务security.context.当AlbumHandler调用时,模拟在该security.context步骤失败.

错误:

 PHP Fatal error:  Call to a member function getToken() on a non-object
Run Code Online (Sandbox Code Playgroud)

码:

     public function setUp()
     {
        $this->container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerInterface');
     }


      public function testAdd()                                                                 
      { 

        // The user I want to return                                                                                        
        $user = $this->getMock('\MyProject\Bundle\UserBundle\Entity\User');  

        // I create a Token for mock getUser()                    
        $token = $this->getMock('\Symfony\Component\Security\Core\Authentication\Token');       
        $token->expects($this->once())                                                          
              ->method('getUser')                                                               
              ->will($this->returnValue($user));                                                

        // I mock the service. PHPUnit don't return an error here.
        $service = $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContextInterface')
              ->disableOriginalConstructor()                                                    
              ->getMock();  
        $service->expects($this->once())                                                        
              ->method('getToken')                                                              
              ->will($this->returnValue($token));                                               

        // I replace the …
Run Code Online (Sandbox Code Playgroud)

php testing phpunit unit-testing symfony

1
推荐指数
1
解决办法
1874
查看次数

标签 统计

php ×1

phpunit ×1

symfony ×1

testing ×1

unit-testing ×1