gpt4 book ai didi

php - 测试时在模型上设置属性

转载 作者:可可西里 更新时间:2023-10-31 23:30:41 25 4
gpt4 key购买 nike

我正在尝试测试 Controller 并模拟模型。在加载 View 之前一切似乎都很顺利,它无法检索应该通过关系加载的该 View 的属性。

我已经尝试在模拟对象上使用 andSet() 设置这些属性,但是这给了我一个错误 getAttribute() does not exist on this mocked object..

这是我的 Controller 方法。

public function __construct(ApplicationRepositoryInterface $application)
{
$this->beforeFilter('consumer_application');

$this->application = $application;
}

public function edit($application_id)
{
$application = $this->application->find($application_id);

$is_consumer = Auth::user()->isAdmin() ? 'false' : 'true';

return View::make('application.edit')
->with('application', $application)
->with('is_consumer', $is_consumer)
->with('consumer', $application->consumer);
}

还有我的测试...

public function setUp()
{
parent::setUp();
$this->mock = Mockery::mock($this->app->make('ApplicationRepositoryInterface'));
}

public function testEdit()
{
$this->app->instance('ApplicationRepositoryInterface', $this->mock);

$this->mock
->shouldReceive('find')
->once()
->andReturn(Mockery::mock('Application'))
->andSet('consumer', Mockery::mock('Consumer'));

Auth::shouldReceive('user')
->once()
->andReturn(Mockery::mock(array('isAdmin' => 'true')));

$application_id = Application::first()->id;
$this->call('GET', 'consumer/application/'.$application_id.'/edit');

$this->assertResponseOk();
$this->assertViewHas('application');
$this->assertViewHas('is_consumer');
$this->assertViewHas('consumer');
}

我得到的最远的是删除 andSet() 部分,它负责 getAttribute() does not exist on this mock object 但它告诉我consumer 在加载 View 时未定义,但仍然失败。

最佳答案

你应该改变:

 Auth::shouldReceive('user')
->once()
->andReturn(Mockery::mock(array('isAdmin' => 'true')));

对此:

 Auth::shouldReceive('user->isAdmin')
->once()
->andReturn('true');

关于php - 测试时在模型上设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27554795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com