PHPUNIT MOCK設定時間 expectation 覆蓋

King_JW發表於2020-09-27
public function testOverrideExpectation()
    {
        // 如果沒有指定 expectation 呼叫次數, 那麼每次呼叫 mock 的 expectation 方法都會返回第一個 expectation
        $mock = \Mockery::mock();
        $mock->shouldReceive('foo')->andReturn('green');
        $mock->shouldReceive('foo')->andReturn('blue');
        $this->assertEquals('green', $mock->foo());
        // 第一個 expectation 指定了只呼叫一次, 所以下面第二次呼叫的時候返回了 blue
        $mock = \Mockery::mock();
        $mock->shouldReceive('foo')->once()->andReturn('green');
        $mock->shouldReceive('foo')->andReturn('blue');
        $this->assertEquals('green', $mock->foo());
        $this->assertEquals('blue', $mock->foo());
    }
public function test_items_can_expire()
    {
        Carbon::setTestNow(Carbon::now());
        $store = new ArrayStore;

        $store->put('foo', 'bar', 10);
        Carbon::setTestNow(Carbon::now()->addSeconds(10)->addSecond());
        $result = $store->get('foo');

        $this->assertNull($result);
        Carbon::setTestNow(null);
    }

摘自原文www.cnblogs.com/eleven24/p/1064049...

本作品採用《CC 協議》,轉載必須註明作者和本文連結
周嘉偉

相關文章