2013-11-20 09:56
浏览 55


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

my service provider is:

class GolfmanagerServiceProvider extends ServiceProvider {

    public function register()


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']) {



    $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');
        ), $book)
 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:


    点赞 打赏 评论

相关推荐 更多相似问题