未找到 Laravel 测试模拟对象方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20092612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel testing mocked object method not found
提问by Ray
Ok - my rocky road journey into testing (with laravel) continues...
好的 - 我的测试(使用 laravel)的艰难旅程仍在继续......
I have created an instance and a 'repository' which I'm now trying to test. However on doing so I get an error that the method in the class is not found. WHich to me implies the class was found at least.
我已经创建了一个实例和一个“存储库”,我现在正在尝试对其进行测试。但是,这样做时我收到一个错误,即找不到类中的方法。对我来说,这意味着至少找到了该课程。
I have added the following to config/app.php:
我在 config/app.php 中添加了以下内容:
//custom service providers
'GolfmanagerServiceProvider'
my service provider is:
我的服务提供商是:
class GolfmanagerServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind(
'golfmanager\service\creator\TicketCreatorInterface',
'golfmanager\service\creator\TicketCreator'
);
}
}
my interface is:
我的界面是:
interface TicketCreatorInterface {
public function createTicket($input, $book);
}
My 'repository' (is that the right term?):
我的“存储库” (这是正确的术语吗?):
Class TicketCreator implements TicketCreatorInterface {
protected $ticket;
public function __construct(TicketAudit $ticketAudit)
{
$this->ticket = $ticketAudit;
}
public function createTicket($input, $book) {
$counter = $input['start'];
while($counter <= $input['end']) {
$this->$ticket->create(array(
'ticketnumber'=>$counter,
'status'=>'unused',
'active'=>1
));
$this->ticket->book()->associate($book);
$counter = $counter+1;
}
}
}
TicketAudit is the eloquent model
TicketAudit 是雄辩的模型
My test so far is:
到目前为止,我的测试是:
public function testCreateTicketBindsTicketAuditFromRepository()
{
// Arrange...
$repository = Mockery::mock('TicketAudit');
$ticketCreator = Mockery::mock('TicketCreatorInterface');
$book = Mockery::mock('Book');
$repository->shouldReceive('create')
->with(array(
'ticketnumber'=>1000,
'status'=>'unused',
'active'=>1
), $book)
->times(2)->andReturn("true");
$book->shouldReceive('find')->once()->andReturn(1);
App::instance('TicketCreatorInterface', $repository);
// Act...
$response = $ticketCreator->createTicket(array('start'=>1000, 'end'=>1001), $book);
// Assert...
//still got to do this bit....
}
I'm not sure whether app instance should be there - have I already done this through the service provider?
我不确定应用程序实例是否应该在那里 - 我是否已经通过服务提供商完成了这项工作?
My error is:
我的错误是:
BadMethodCallException: Method TicketCreatorInterface::createTicket() does not exist on this mock object
I am very new to testing - and this approach (creating interfaces) so have been picking up bits from tutorials and books - it's not clicking yet what exactly should be going on here as learning as I go
我对测试非常陌生 - 这种方法(创建界面)所以一直在从教程和书籍中学到一些东西 - 它尚未点击,在我学习的过程中到底应该发生什么
What silly mistake have I made this time??
这次我犯了什么愚蠢的错误??
I have done composer update, composer install and composer dump-autoload but no effect so far.
我已经完成了 composer update、composer install 和 composer dump-autoload 但到目前为止没有任何效果。
回答by Manuel Pedrera
You have mocked your interface
你嘲笑了你的界面
$ticketCreator = Mockery::mock('TicketCreatorInterface');
$ticketCreator = Mockery::mock('TicketCreatorInterface');
and you called createTicket()
on that mock object
然后你调用createTicket()
了那个模拟对象
$response = $ticketCreator->createTicket(array('start'=>1000, 'end'=>1001), $book);
$response = $ticketCreator->createTicket(array('start'=>1000, 'end'=>1001), $book);
However, you forgot to tell Mockery that the method createTicket()
is going to be called and should be mocked:
但是,您忘记告诉 Mockery 该方法createTicket()
将被调用并且应该被模拟:
$ticketCreator->shouldReceive('createTicket')->once()->andReturn($whatever);
$ticketCreator->shouldReceive('createTicket')->once()->andReturn($whatever);