Answer the question
In order to leave comments, you need to log in
How to make a mock of your own service class in a test?
I have a controller -
class PostController extends Controller
{
private $postindexer;
public function __construct(PostIndexer $postindexer)
{
$this->postindexer = $postindexer;
}
public function store(Request $request)
{
$post = new Post;
$post->name = $request->name;
$post->save();
$this->postindexer->index($post);
return response()->json($post, 201);
}
}
public function testStore()
{
$params = [ 'title' => 'New Post', 'body' => "I'm a content"];
$this->post('/admin/posts', $params)->assertStatus(201);
$post = Post::first()->toArray();
$this->assertDatabaseHas('posts', $post);
}
$this->postindexer->index($post);
Answer the question
In order to leave comments, you need to log in
https://laravel.com/docs/5.8/mocking#mocking-objects
$this->mock(PostIndexer::class, function ($mock) {
$mock->shouldReceive('index')->once();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question