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 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.