dsg435665475 2014-08-23 16:18
浏览 5
已采纳

PhpSpec在模拟时返回null

I'm working with PhpSpec and for some reason when I mock my dependencies and call them the willReturn method of PhpSpec give me a null value instead of the value passed.

This is the method that I'm trying to describe

    /**
     * Register an User
     *
     * @param array $infoUser
     * @return User
     */
    public function register(array $infoUser)
    {

        $user = $this->user->create($infoUser);

        $this->raise(new UserRegistered($user));

        return $user;
    }

My Spec

class BaseAuthSpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType('Core\Auth\BaseAuth');
    }

    function let(AuthManager $guard,UserAuthRepository $user)
    {
        $this->beConstructedWith($guard,$user);
    }

    function it_register_an_user(UserAuthRepository $useRepo)
    {
        $user = [
            'username' => 'fabri',
            'email'    => 'test@test.com',
            'password' => 'password',
            'repeat_password' => 'password'
        ];

        $userModel = new User($user);

        // this line return null instead the $userModel
        $useRepo->create($user)->shouldBeCalled()->willReturn($userModel);

        $this->raise(new UserRegistered($userModel))->shouldReturn(null);

        $this->register($user)->shouldReturn($userModel);
    }
}

I'm stuck with this issue, any suggest will be appreciated.

  • 写回答

1条回答 默认 最新

  • duanduoding2238 2014-09-08 20:27
    关注

    Arguments are matched by name. The user repository passed to your let() method, is not the same as passed to the it_register_an_user() method. To fix your issue, simply give it the same name.

    There're other issues in your spec.

    It's not possible to mock nor stub a method on the class you're speccing. This is not going to work:

    $this->raise(new UserRegistered($userModel))->shouldReturn(null);
    

    I'm not sure what's going on in the raise() method, but you should deal with it properly in your example, so either stub or mock any collaborators (or leave them alone if there's no return values relevant to the current example).

    Another thing is that you use mocks when what you really need is stubs. I'd rewrite your example to be:

    class BaseAuthSpec extends ObjectBehavior
    {
        function let(AuthManager $guard, UserAuthRepository $userRepo)
        {
            $this->beConstructedWith($guard, $user);
        }
    
        function it_registers_a_user(UserAuthRepository $userRepo, User $userModel)
        {
            $user = [
                'username' => 'fabri',
                'email'    => 'test@test.com',
                'password' => 'password',
                'repeat_password' => 'password'
            ];
    
            $userRepo->create($user)->willReturn($userModel);
    
            $this->register($user)->shouldReturn($userModel);
        }
    }
    

    The raise method should be covered by seperate examples.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题