在SECOND测试设置时找不到容器中的服务(首先是工作)

时间:2014-01-24 10:21:07

标签: symfony phpunit

我写了一个带有更长setUp的测试用例,看起来像这样:

public function setUp(){

        $this->manager = $this->get('strego_tipp.invalidation_manager');

        $mockEm = m::mock('Doctrine\Common\Persistence\ObjectManager');
        $this->setAttribute($this->manager, 'em', $mockEm);

        /* .... additional setup stuff ....*/

}


protected function get($service){
        return $this->getContainer()->get($service);
}

这只是设置,现在我的testfile中有2个测试:

public function test_it_collects_not_duplicate_betRounds(){
        $this->assertCount(0, $this->manager->betRounds);
        $this->call($this->manager,'collectBetRoundWorkload', $this->game1);
        $this->call($this->manager,'collectBetRoundWorkload', $this->game2);
        $this->manager->collectBetRoundWorkload($this->game1);
        $this->manager->collectBetRoundWorkload($this->game2);
        $this->assertCount(2, $this->manager->betRounds);

    }

    public function test_it_collects_all_bets_to_update(){
        $this->assertCount(0, $this->manager->bets);
        $this->call($this->manager,'collectBetWorkload', $this->game1);
        $this->manager->collectBetWorkload($this->game1);
        $this->assertCount(3, $this->manager->bets);   
    }

运行测试时,第二个测试用例总是失败,因为在设置中,容器中没有名为“strego_tipp.invalidation_manager”的服务。虽然该服务在第一次测试中可用。它也独立于实际的测试用例,因为我可以更改测试的顺序,但服务不在第二个测试用例的容器中。在我看来,这似乎是一个非常随机的问题,我无法理解它。

1 个答案:

答案 0 :(得分:0)

我通过清理缓存解决了这个问题,因为我在prod环境中:

$app/console cache:clear --env=prod

希望有所帮助:)