Answer the question
In order to leave comments, you need to log in
How to properly test controller methods that interact with the database in Laravel?
Friends, hello everyone.
We were given the task of writing unit tests for controllers in Laravel.
Tests on assertStatus, assertViewHas - wrote.
Now we need to write tests for the rest of the controller methods, but there are questions, googling, I did not find exhaustive answers .. Help, please!
Here are some test methods:
1.
class BugsController extends Controller
{
public function track(Request $request) {
if(Bug::create([
'title' => $request->data['title'],
'note' => $request->data['note']
])) { return ['data' => 'Bug tracked']; }
else { return ['data' => 'Something went wrong']; }
}
class AccountsController extends Controller
{
public function status(Request $request)
{
$id = $request->input('account_id');
$status = $request->input('status');
$account = Account::where('id', $id)->first();
if ($account->update(['user_status_id' => $status])) {
\Session::flash('status', 'Status updated');
} else {
\Session::flash('error', 'Something went wrong');
}
return redirect()->route('accountlist');
}
}
Answer the question
In order to leave comments, you need to log in
And why the test DB does not approach?
It's just that when you run the tests, write a script that dumps the combat base (it's better, of course, to generate it through Faker)
into the test one and check there, change what you want.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question