即使存在匹配,Where子句也不返回活动记录中的任何记录

时间:2015-06-23 18:39:45

标签: ruby-on-rails activerecord rspec

我正在尝试为我的代码编写测试。类方法如下。

def get_alerts
  p JobSearch.all
  p JobSearch.where('cancelled_date is null and created_at > ? and email_address is not null', DateTime.now - 90)
  JobSearch.where('cancelled_date is null and created_at > ? and email_address is not null', DateTime.now - 90)
end

我的测试看起来像......

it 'returns alerts with a null cancelled_date' do
  JobSearch.create!(cancelled_date: DateTime.now, email_address: 'test@test.com', job_board_id: job_board.id, created_at: DateTime.now - 80)
  JobSearch.create!(cancelled_date: DateTime.now, email_address: 'test@test.com', job_board_id: job_board.id, created_at: DateTime.now - 80)
  JobSearch.create!(job_board_id: job_board.id, email_address: 'test@test.com', created_at: DateTime.now - 2)
  JobSearch.create!(job_board_id: job_board.id, email_address: 'test@test.com', created_at: DateTime.now - 2)
  JobSearch.create!(job_board_id: job_board.id, email_address: 'test@test.com', created_at: DateTime.now - 1)
  alerts = task.get_alerts
  expect(alerts.count).to be(3)
end

当我运行测试时,我从get_alerts方法得到nil。但是,为JobSearch.all查询写入控制台的内容为我提供了以下内容。

<JobSearch id: 1, keyword: nil, pageSize: 0, showAllJobs: false, page: 0, position_title: nil, job_type: nil, salary: nil, include_remote: false, filter_location: nil, radius_location: nil, salary_min: nil, salary_max: nil, email_address: "test@test.com", job_id: nil, cancelled_date: "2015-06-23 18:32:29", job_board_id: 1, last_run_date: nil, added_since: nil, created_at: "2015-04-04 18:32:29">, #<JobSearch id: 2, keyword: nil, pageSize: 0, showAllJobs: false, page: 0, position_title: nil, job_type: nil, salary: nil, include_remote: false, filter_location: nil, radius_location: nil, salary_min: nil, salary_max: nil, email_address: "test@test.com", job_id: nil, cancelled_date: "2015-06-23 18:32:29", job_board_id: 1, last_run_date: nil, added_since: nil, created_at: "2015-04-04 18:32:29">, #<JobSearch id: 3, keyword: nil, pageSize: 0, showAllJobs: false, page: 0, position_title: nil, job_type: nil, salary: nil, include_remote: false, filter_location: nil, radius_location: nil, salary_min: nil, salary_max: nil, email_address: "test@test.com", job_id: nil, cancelled_date: nil, job_board_id: 1, last_run_date: nil, added_since: nil, created_at: "2015-06-21 18:32:29">, #<JobSearch id: 4, keyword: nil, pageSize: 0, showAllJobs: false, page: 0, position_title: nil, job_type: nil, salary: nil, include_remote: false, filter_location: nil, radius_location: nil, salary_min: nil, salary_max: nil, email_address: "test@test.com", job_id: nil, cancelled_date: nil, job_board_id: 1, last_run_date: nil, added_since: nil, created_at: "2015-06-21 18:32:29">, #<JobSearch id: 5, keyword: nil, pageSize: 0, showAllJobs: false, page: 0, position_title: nil, job_type: nil, salary: nil, include_remote: false, filter_location: nil, radius_location: nil, salary_min: nil, salary_max: nil, email_address: "test@test.com", job_id: nil, cancelled_date: nil, job_board_id: 1, last_run_date: nil, added_since: nil, created_at: "2015-06-22 18:32:29">]>

那么我在where子句中做错了什么?

0 个答案:

没有答案