我正在使用Rails 5.1.6和Ruby 2.5.1(虽然在以前的版本中有相同的错误)。
将邮件从deliver_now切换到deliver_later。在我的浏览器中正常工作,但是我一直试图让我的集成测试工作。看似使用ActiveJob :: TestHelper中的任何方法会触发以下错误:
错误:SendJobToContactsTest#test_send_job_to_contacts: SystemStackError:堆栈级别太深
集成测试代码:
<?php
error_reporting(E_ALL ^ E_NOTICE);
class MUser extends CI_Model {
public $table = "user";
public function _construct()
{
parent::_construct();
}
public function CheckUser($username, $password) {
$query = $this->db->get_where($this->table, array('username'=>$username, 'password'=>$password));
if($query->num_rows() > 0)
{
return true;
} else {
return false;
}
}
}
?>
这很好用:
require 'test_helper'
include ActiveJob::TestHelper
class SendJobToContactsTest < ActionDispatch::IntegrationTest
...
end
使用assert_difference 'ContactJob.count', 1 do
patch update_contacts_user_job_path(@user, @job), params: {job: {contact_ids: [ @contact2.id], message: {message_text: "This is a test"}}}
end
触发SystemStackError:
performed_enqueued_jobs
即使只有assert_difference 'ContactJob.count', 1 do
perform_enqueued_jobs do
patch update_contacts_user_job_path(@user, @job), params: {job: {contact_ids: [ @contact2.id], message: {message_text: "This is a test"}}}
end
end
没有assert_enqueued_jobs
,也会触发SystemStackError:
performed_enqueued_jobs
事实证明,即使只调用一个空的perform_enqueued_jobs块(不涉及我的代码)也会触发堆栈级别太深的问题:
assert_difference 'ContactJob.count', 1 do
patch update_contacts_user_job_path(@user, @job), params: {job: {contact_ids: [ @contact2.id], message: {message_text: "This is a test"}}}
assert_enqueued_jobs 1
end
答案 0 :(得分:1)
原来问题在于我有include语句。在课堂上移动它解决了这个问题:
require 'test_helper'
class SendJobToContactsTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
答案 1 :(得分:0)
我使用spec语法遇到了同样的问题:
之前:
require 'test_helper'
include ActiveJob::TestHelper
describe MyJob do
...
修复:
require 'test_helper'
describe MyJob do
include ActiveJob::TestHelper