如果正在运行测试,则默认使用Laravel disables public function build()
{
$event = $this->createEvent($this->data[2]->planned_at, $this->data[2]->subject, $this->data[2]->content);
$path=realpath('app/public/events/' . $this->data[2]->subject . '.ics');
return $this->view('emails.template')
->from('info@stackoverflow.nl', 'Stackoverflow')->subject($this->data[2]->subject)
->with([
'token' => $this->data[0],
'email' => $this->data[1],
'mail' => $this->data[2],
'sponsors' => $this->data[3],
'name' => $this->data[4],
])
->attach($path), [
'as' => $this->data[2]->subject . '.ics',
'mime' => 'calendar/event',
]);
}
中间件。结果我没有注意到api路由需要csrf令牌才能成功。有没有办法让它恢复一些测试?像
VerifyCsrfToken
并使用function withVerifyCsrfTokenMiddleware()
{
$this->app... // here we're passing something to the app
}
' VerifyCsrfToken
方法进行修改。在一个自定义的。一个,从框架中覆盖该方法。但这只是我的想法。还有更好的吗?
答案 0 :(得分:0)
好的,这就是我的所作所为:
app/Http/Middleware/VerifyCsrfToken.php
:
function handle($request, Closure $next)
{
$dontSkipCsrfVerificationWhenRunningTests
= $this->app->bound('dontSkipCsrfVerificationWhenRunningTests')
&& $this->app->make('dontSkipCsrfVerificationWhenRunningTests');
if (
$this->isReading($request) ||
! $dontSkipCsrfVerificationWhenRunningTests && $this->runningUnitTests() ||
$this->shouldPassThrough($request) ||
$this->tokensMatch($request)
) {
return $this->addCookieToResponse($request, $next($request));
}
throw new TokenMismatchException;
}
tests/TestCase.php
:
function withVerifyCsrfTokenMiddleware()
{
$this->app->instance('dontSkipCsrfVerificationWhenRunningTests', TRUE);
}
答案 1 :(得分:0)
对上述答案的小改动,而不是更改 handle 方法,我更新了 runningUnitTests 方法以减少升级时出现问题的可能性:
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
];
/**
* Determine if the application is running unit tests.
*
* @return bool
*/
protected function runningUnitTests()
{
$dontSkip
= $this->app->bound('dontSkipCsrfVerificationWhenRunningTests')
&& $this->app->make('dontSkipCsrfVerificationWhenRunningTests');
return $this->app->runningInConsole() && $this->app->runningUnitTests() && !$dontSkip;
}
}