Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got an error when writing tests #1

Open
ghost opened this issue Mar 15, 2018 · 2 comments
Open

Got an error when writing tests #1

ghost opened this issue Mar 15, 2018 · 2 comments

Comments

@ghost
Copy link

ghost commented Mar 15, 2018

I don't know what's the problem, but it only appears when using this package.

When testing, i have the following code:

$user = factory(User::class)->make([
    'email' => 'notAnEmail'
]);

$this->assertFalse($user->isValid());

This works fine, but, if i change my test to not assert, like:

$user = factory(User::class)->make([
    'email' => 'john@example.com'
]);

$this->assertFalse($user->isValid());

Then i get the following error on console:

Error: Cannot use object of type Illuminate\Support\Facades\Config as array
@jarektkaczyk
Copy link
Owner

Hi,
provide context so we can reproduce the error. Laravel version, your code etc.

@ghost
Copy link
Author

ghost commented Mar 19, 2018

I'm using a brand new installation of Laravel 5.6. (created using the "laravel new MyProject" command).

Then i installed your package using composer:

(composer.json)

"require": {
  "php": "^7.1.3",
  "doctrine/dbal": "^2.6",
  "fideloper/proxy": "^4.0",
  "laravel/framework": "5.6.*",
  "laravel/tinker": "^1.0",
  "sofa/eloquence": "^5.6"
},

Then i updated my User class as described in docs:

User.php

<?php namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Sofa\Eloquence\Eloquence; // base trait
use Sofa\Eloquence\Validable; // extension trait
use Sofa\Eloquence\Contracts\CleansAttributes;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;

class User extends Authenticatable implements ValidableContract, CleansAttributes {

  use Notifiable, SoftDeletes, Eloquence, Validable;

  protected $fillable = [
    'email', 'password',
  ];

  protected $hidden = [
    'password', 'remember_token',
  ];

  protected $dates = [
     'deleted_at'
   ];

  protected $rules = [
    'email'    => 'email|required|unique',
    'password' => 'required'
   ];
}

Then i created a test using the php artisan make:test UserTest command:

tests/Feature/UserTest.php

<?php

namespace Tests\Feature;

use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class UserTest extends TestCase
{
	use RefreshDatabase;

	public function testUserRegistration()
	{
		$created = factory(User::class)->create();

		$this->assertInstanceOf(User::class, $created);
	}

	public function testUserValidation()
	{
		$user = factory(User::class)->make();

		$this->assertTrue($user->isValid()); // and here i got the error
	}
}

If i change the testUserValidation method to perform an invalid request, then the assertion happens ok. Like:

	public function testUserValidation()
	{
            $user = factory(User::class)->make([
                'email' => null
            ]);

	    $this->assertTrue($user->isValid()); // no errors, only testing fails, so its ok.
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant