Question

I'm writing some unit tests for a simple model which only has two fields: question_id and title.

I've set the fillable array on the model to include only the title:

protected $fillable = array('title');

I've also created the following unit test:

    /**
     * @expectedException Illuminate\Database\Eloquent\MassAssignmentException
     */
    public function testAnswerQuestionIdCanNotBeMassAssigned()
    {
        $params = ['question_id' => 1, 'title' => 'something'];
        $answer = new Answer($params);
    }

However,no exception is thrown, causing the test to fail.

Am I missing something here?

Any advice appreciated.

Thanks.

Was it helpful?

Solution

you can see why in the fill method of the Model

public function fill(array $attributes)
{
    $totallyGuarded = $this->totallyGuarded();

    foreach ($this->fillableFromArray($attributes) as $key => $value)
    {
        $key = $this->removeTableFromKey($key);

        // The developers may choose to place some attributes in the "fillable"
        // array, which means only those attributes may be set through mass
        // assignment to the model, and all others will just be ignored.
        if ($this->isFillable($key))
        {
            $this->setAttribute($key, $value);
        }
        elseif ($totallyGuarded)
        {
            throw new MassAssignmentException($key); <--- only if totally guarded
        }
    }

    return $this;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top