2

What is the correct syntax to post 'AccountApiKey[merchant_id]' in the below example for testing insertion in Laravel 5.4.

In production, the code works fine without any issues, in tests I get the error "table not found" where it is not inserting correctly. I've asked in IRC but it stumped them so I'm hoping the wonderful community of stackoverflow can assist.

Thanks!

View:

{{-- Merchant ID form input --}}
<div class="form-group">
    {!! Form::label('AccountApiKey[merchant_id]', 'Seller ID:') !!}
    {!! Form::text('AccountApiKey[merchant_id]', null, ['class' => 'form-control']) !!}
</div>

Test (See below update for complete test):

    $accountApiKey = factory(AccountApiKey::class)->make([
        'merchant_id' => 'test'
    ]);

$this->post($this->urlToUse(), [
            'name' => $account->name,
            'AccountApiKey[merchant_id]' => $accountApiKey->merchant_id,
]);



    $this->assertDatabaseHas('account_api_keys', [
        'merchant_id' => $accountApiKey->merchant_id,
]);

Controller:

   $account->accountApiKey()->save(new AccountApiKey($request->get('AccountApiKey')));

Update as per Sandeesh comment:

Model:

class AccountApiKey extends Model implements Transformable
{
    use TransformableTrait;

    protected $fillable = ['last_modified_user_id', 'merchant_id'];

    protected $events = ['saving' => SettingsUpdated::class];

    public function account()
    {
        return $this->belongsTo('App\Models\Account\Settings\Accounts');
    }

}

Complete Test:

 class StoreTest extends TestCase implements TestInterface
{

    use DatabaseMigrations;

    /**
     * Tests all forms are inserting correct into database
     */
    public function test_inserting_into_database()
    {
        $user1 = $this->userAndCompany();

        $this->actingAs($user1);

        $account = factory(Account::class)->create([
            'company_id' => $user1->company()->first()->id,
        ]);

        $accountApiKey = factory(Account\AccountApiKey::class)->make([
            'last_modified_user' => $user1->id,
            'account_id' => $account->id,
            'merchant_id' => 'test'
        ]);


        $this->post($this->urlToUse(), [
            'name' => $account->name,
            'AccountApiKey[merchant_id]' => $accountApiKey->merchant_id,
            ]);

        $this->assertDatabaseHas('accounts', [
            'name' => $account->name, //asserts true
        ]);

        $this->assertDatabaseHas('account_api_keys', [
            'merchant_id' => $accountApiKey->merchant_id, // asserts false
        ]);

    }

        /**
     * The url which is under test
     * @return mixed
     */
    function urlToUse()
    {
        return 'account/settings/account';
    }

}
7
  • Have you configured the test database? Commented May 18, 2017 at 16:40
  • Yes, test database is using :memory: other db tests work fine and the line: 'name' => $account->name (in post) asserts true (so it is inserting) Commented May 18, 2017 at 16:41
  • can you share your AccountApiKey model and structure along with the complete test. Commented May 18, 2017 at 16:45
  • @Sandeesh updated Commented May 18, 2017 at 17:19
  • Are you sure your testing database is up to date? Might be worth running php artisan migrate --database=testing to be sure Commented May 18, 2017 at 17:29

1 Answer 1

3

Ok here it is, i found the issue and you have to change your post data to this for the test to work.

$this->post($this->urlToUse(), [
    'name' => $account->name,
    'AccountApiKey' => [
        'merchant_id' => $accountApiKey->merchant_id,
    ],
]);

At the same time, you're unnecessarily making using of an array input which could use a normal input. If you want then i'll give you an advice to cleanup the code a bit.

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

Comments

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.