2

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 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - that works - I'm still trying to understand testing - your explanations have helped Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.