Hot to spec behaviour that calls class methods

43 views
Skip to first unread message

Gonzalo Vilaseca

unread,
Aug 7, 2014, 4:59:33 AM8/7/14
to phpsp...@googlegroups.com
So I have this method:

     public function newWishlist($user)
    {
        $wishlist = $this->createNew();

        $wishlist->setUser($user);

        $this->persistAndFlush($wishlist);

        return $wishlist;
    }

My first approach was to spec it this way:

        $this->createNew()->willReturn($wishlist);

        $wishlist->setUser($user)->shouldBeCalled()->willReturn($wishlist);

        $this->persistAndFlush($wishlist)->shouldBeCalled();

        $this->newWishlist($user);

But this throwed an error because I was mocking calls to the SUS, so I had to follow another approach:

       $wishlist = $this->newWishlist($user);
     
       $wishlist->getUser()->shouldReturn($user);

Is this second approach correct? Instead of mocking, I'm checking behaviour after the call.
      
Reply all
Reply to author
Forward
0 new messages