I gave a getFooAttribute
method that returns a value depending on the format of some field.
public function getFooAttribute()
{
$re = 'some regex /';
if(preg_match($re, $this->bar) === 1){
return 'normal'. $this->bar;
}
return 'not normal'. $this->bar;
}
Now I would like to write tests for when a field is a certain value. How can I do this?
CodePudding user response:
You can write a test case like this:
use Illuminate\Support\Str;
public function test_foo_attribute()
{
$model = YourModel::factory()->make();
$model->bar = 'should be normal';
$this->assertTrue(Str::startsWith($model->bar, 'normal'));
$model->bar = 'should be not normal';
$this->assertTrue(Str::startsWith($model->bar, 'not normal'));
}