Category : tdd

I Have a question. I’m working with a Laravel project that only has models, the corresponding migrations are in other "micro projects". I’m developing TDD, and in many cases I need to use factories to test some functionalities…but, when I run the test, messages like this, appears: General error: 1 no such table: users Can ..

Read more

I ran this code snippet. /** @test */ public function unauthenticated_users_cannot_delete_articles() { $article = Article::factory()->create(); $this->delete($article->path()) ->assertRedirect(route(‘login’)); $this->signIn(); $this->delete($article->path()) ->assertStatus(403); } And got this message: Response status code [419] is not a redirect status code. When I turned off the csrf middleware at the route, this test returned green. Isn’t csrf middleware disabled for all ..

Read more

I don’t know what caused the error(ErrorException : Trying to get property ‘visits’ of non-object) after running the code snippet below. And the output pinpointed out that it was the last assertion that triggered the error. /** @test */ public function a_reading_of_an_article_can_be_recorded() { $article = Article::factory()->create(); $this->assertSame(0, $article->visits); $this->call(‘GET’, $article->path()); $this->assertEquals(1, $article->fresh()->visits); } When hitting ..

Read more

I have a Mail fie called EmailGenerator and I have test called NotificationTest. Everytime I run the tests, I’m always getting this error. Object of class AppMailEmailGenerator could not be converted to string Here’s my NotificationTest.php <?php namespace TestsUnit; use TestsTestCase; use AppHttpLivewireNotificationsCreateNotification; use AppHttpLivewireNotificationsManageNotifications; use AppModelsEmailTemplate; use LivewireLivewire; use Mockery; use MockeryMockInterface; use Mail; ..

Read more

I have a pretty simple test to check my event increases the count of a video play $video = Video::factory()->create([ ‘uuid’ => ‘xxx’, ‘slug’ => ‘xxx’ ]); $event = new VideoPlayWasStarted($video->vimeo_id); $listener = new IncreaseVideoStartedCount; $listener->handle($event); $this->assertEquals(++$video->started_plays, $video->fresh()->started_plays); in VideoPlaysWasStarted class, I pass in the video public $videoId; public function __construct($videoId) { $this->videoId = $videoId; ..

Read more

That’s the code for my unit test, the default one: <?php namespace TestsFeature; use IlluminateFoundationTestingRefreshDatabase; use TestsTestCase; class ExampleTest extends TestCase { /** * A basic test example. * * @return void */ public function testBasicTest() { $response = $this->get(‘/’); $response->assertStatus(200); } } I’m running the laravel project on: php artisan serv When I manually ..

Read more

I have the following written the following TEST in tests/Feature/emailPollTest.php … …. Mail::fake(); $ja=["id"=>1, ‘status’=>’error’,’log’=>’log test’]; $response = $this->post(‘/api/store’,$data); $response ->assertStatus(201); $response->assertJson($ja); $order = new NotifyPoll($ja); Mail::assertSent(function (NotifyPoll $mail) use ($order) { $count = 0; if($mail->itemPoll[‘id’] === $order->itemPoll[‘id’]) $count++; if($mail->itemPoll[‘status’] === $order->itemPoll[‘status’]) $count++; if($mail->itemPoll[‘log’] === $order->itemPoll[‘log’]) $count++; return 3 === $count; }); Mail::assertSent(NotifyPoll::class, 1); can ..

Read more

I need to know how to assert that Laravel Controller returns view with proper data. My simple controller function: public function index() { $users = User::all(); return view(‘user.index’, [‘users’ => $users]); } I am using functions such as assertViewIs to get know if proper view file is loaded: $response->assertViewIs(‘user.index’); Also using asserViewHas to know that ..

Read more

hi m following laracasts forum but when i enter this command $threads->each(function ($thread) { factory(‘AppReply’, 10)->create([‘thread_id’ => $thread->id]); }); then it shows this error: InvalidArgumentException with message ‘Unable to locate factory with name [default] [App/Reply].’ $threads->each(function ($thread) { factory(‘AppReply’, 10)->create([‘thread_id’ => $thread->id]); }); $factory->define(Thread::class, function ($faker) { return [ ‘user_id’ => function() { return factory(‘AppUser’)->create()->id; ..

Read more

I have a test asserting that images can be uploaded. Here is the code… // Test $file = UploadedFile::fake()->image(‘image_one.jpg’); Storage::fake(‘public’); $response = $this->post(‘/api/images’, [ ‘images’ => $file ]); Then in controller i am doing something simpler.. $file->store(‘images’, ‘public’); And asserting couple of things. and it works like charm. But now i need to resize the ..

Read more