简单测试发现奇怪的错误

发布于 2024-10-15 10:11:05 字数 3065 浏览 3 评论 0原文

这就是我正在测试的:

模型

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 [email protected]", 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 [email protected]", 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 [email protected]", 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 [email protected]", locked: false, visits: 0>]
     # ./spec/models/job_spec.rb:91:in `block (3 levels) in <top (required)>'

我缺少什么?看起来一切都好,对吧?我不明白那里出了什么问题。

this is what I'm testing:

model

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

test

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

the first test is passing, but I can't pass on second. The following error message is returning:

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 [email protected]", 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 [email protected]", 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 [email protected]", 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 [email protected]", locked: false, visits: 0>]
     # ./spec/models/job_spec.rb:91:in `block (3 levels) in <top (required)>'

what I missing? it's look all okay, right? I can't understand whats wrong there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-10-22 10:11:05

看来您的作用域正在返回一个数组(这是我所期望的)。您的测试可能应如下所示:

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

It appears that your scope is returning an array (which is what I would expect). Your test should probably look like:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文