dougao9864 2013-11-20 09:56
浏览 57
已采纳

Laravel测试模拟对象方法未找到

Ok - my rocky road journey into testing (with laravel) continues...

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:

//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

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.

  • 写回答

1条回答 默认 最新

  • duanben4771 2013-11-20 11:03
    关注

    You have mocked your interface

    $ticketCreator = Mockery::mock('TicketCreatorInterface');

    and you called createTicket() on that mock object

    $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:

    $ticketCreator->shouldReceive('createTicket')->once()->andReturn($whatever);

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

报告相同问题?

悬赏问题

  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用