dongshi1102 2014-08-22 23:47
浏览 5
已采纳

如何在phpspec测试中使用laravel会话

I'm trying to test a very simple class with phpspec.

Some methods of the class that should be tested

/**
 * @param Store $session
 */
function __construct(Store $session)
{
    $this->session = $session;
}

/**
 * @param Store $session
 */
function __construct(Store $session)
{
    $this->session = $session;
}

/**
 * Set the current order id
 *
 * @param $orderId
 */
public function setCurrentOrderId($orderId)
{
    $this->session->set($this->sessionVariableName, $orderId);

    return $this;
}

/**
 * Get the current order id
 *
 * @return mixed
 */
public function getCurrentOrderId()
{
    return $this->session->get($this->sessionVariableName);
}

and a piece of the test

use Illuminate\Session\Store;


class CheckoutSpec extends ObjectBehavior
{
    function let(Store $session)
    {
        $this->beConstructedWith($session);
    }

    function it_is_initializable()
    {
        $this->shouldHaveType('Spatie\Checkout\Checkout');
    }

    function it_stores_an_orderId()
    {
        $this->setCurrentOrderId('testvalue');

        $this->getCurrentOrderId()->shouldReturn('testvalue');

    }
}

Unfortunately the test fails on it_stores_an_orderId with this error expected "testvalue", but got null.

When the methods setCurrentOrderId and getCurrentOrderId are used in artisan's tinker they work just fine.

It seems that in my test environment there something wrong with the setup of the session.

How can this problem be solved?

  • 写回答

1条回答 默认 最新

  • drema2014 2014-08-23 06:46
    关注

    You're actually trying to test more than just your class. PHPSpec specs (and unit tests in general) are meant to be run in isolation.

    What you really want in this case is to make sure that your class works as intended, isn't it? Simply mock the Store class and only check that the necessary methods from it are being called and mock their return results if there are any. This way you'll still know that your class works as intended and won't test something that has been thoroughly tested already.

    Here's how you might do it:

    function it_stores_an_orderId(Store $session)
    {
        $store->set('testvalue')->shouldBeCalled();
        $store->get('testvalue')->shouldBeCalled()->willReturn('testvalue');
    
        $this->setCurrentOrderId('testvalue');
        $this->getCurrentOrderId()->shouldReturn('testvalue');
    
    }
    

    If you still want to involve some of the other classes directly, something like Codeception or PHPUnit might be more appropriate, since you can control your testing environment more.

    However, if you still want to do this with PHPSpec, it might be possible with this package (I haven't tried it myself though, so no guarantee).

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

报告相同问题?

悬赏问题

  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块
  • ¥15 nhanes加权logistic回归,svyglm函数