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

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);

    点赞 打赏 评论

相关推荐 更多相似问题