简单测试的奇怪错误

时间:2011-01-31 02:14:09

标签: ruby-on-rails ruby-on-rails-3 rspec2 factory-bot

这就是我正在测试的内容:

模型

scope :user_pending, lambda { |user|
where("jobs.available = 0 AND jobs.user_id = ?", user.id) }

测试

it "should have the scope" do
  Job.should respond_to(:user_pending)
end

it "should return a list of jobs that are unavailable for the current_user" do
  @user = Factory(:user, :id => 1)
  @job = Factory(:job, :available => false, :user_id => @user)
  Job.user_pending(@user).should == @job
end

第一次测试正在通过,但我无法传递第二次。返回以下错误消息:

Failures:


  1) Job Job :user_pending should return a list of jobs that are unavailable for the current_user
     Failure/Error: Job.user_pending(@user).should == @job
     expected: #<Job id: 6, title: "Contrata-se para fabrica de autos", location: "Aichi-ken, Toyohashi-shi", content: "Estamos contratando pessoas interessadas em trabalh...", user_id: 1, created_at: "2011-01-31 02:10:04", updated_at: "2011-01-31 02:10:04", available: false, company_name: "K.K. Test", company_website: "http://www.test.com", how_to_apply: "Enviar email para contato@test.com", locked: false, visits: 0>,
          got: [#<Job id: 6, title: "Contrata-se para fabrica de autos", location: "Aichi-ken, Toyohashi-shi", content: "Estamos contratando pessoas interessadas em trabalh...", user_id: 1, created_at: "2011-01-31 02:10:04", updated_at: "2011-01-31 02:10:04", available: false, company_name: "K.K. Test", company_website: "http://www.test.com", how_to_apply: "Enviar email para contato@test.com", locked: false, visits: 0>] (using ==)
     Diff:
     @@ -1,2 +1,2 @@
     -#<Job id: 6, title: "Contrata-se para fabrica de autos", location: "Aichi-ken, Toyohashi-shi", content: "Estamos contratando pessoas interessadas em trabalh...", user_id: 1, created_at: "2011-01-31 02:10:04", updated_at: "2011-01-31 02:10:04", available: false, company_name: "K.K. Test", company_website: "http://www.test.com", how_to_apply: "Enviar email para contato@test.com", locked: false, visits: 0>
     +[#<Job id: 6, title: "Contrata-se para fabrica de autos", location: "Aichi-ken, Toyohashi-shi", content: "Estamos contratando pessoas interessadas em trabalh...", user_id: 1, created_at: "2011-01-31 02:10:04", updated_at: "2011-01-31 02:10:04", available: false, company_name: "K.K. Test", company_website: "http://www.test.com", how_to_apply: "Enviar email para contato@test.com", locked: false, visits: 0>]
     # ./spec/models/job_spec.rb:91:in `block (3 levels) in <top (required)>'
我错过了什么?看起来一切都好,对吧?我无法理解那里的错误。

1 个答案:

答案 0 :(得分:3)

看来你的范围正在返回一个数组(这是我所期望的)。您的测试应该看起来像:

Job.user_pending(@user).first.should == @job