How to use Mockery for a class used in a controller?

Posted 5 months ago by shadrix

Let's say my controller looks like that.

public function store()
{
    Newsletter::subscribePending($email);
}

And Newsletter is a Class to use MailChimps Api easier.

Now how do I write a test for it?

I tried this, but it does not work. (I'm new to Mockery and it sends the data to mailchimp).

use Spatie\Newsletter\Newsletter;
class NewsletterTest extends TestCase {

    use RefreshDatabase;

    public function setUp()
    {
        parent::setUp();

        \Mockery::mock(Newsletter::class);
    }

    public function tearDown() {
        \Mockery::close();
    }


    /** @test */
    public function a_guest_can_subscribe_to_newsletter() 
    {
        $this->withoutExceptionHandling();
        $this->json('post', route('newsletter.store'), ['email' => '[email protected]'])
             ->assertStatus(201);
    }
}

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.