如何测试范围? [英] How to test scopes?

查看:40
本文介绍了如何测试范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找到但没有成功.只是想知道如何在 Rails 3 中测试作用域.

tried to find but with no success. Just wondering how could I test scopes in Rails 3.

可能使用 rspec、should 或只是一个测试单元.

Could be using rspec, shoulda or just a test unit.

谢谢.

其实我是这样试的,但是还不是完整的测试,因为还需要放order()方法.

Actually, I trying this way, but it's not complete test since it's still need to put the order() method.

范围:

scope :recents_available, where(:available => true, :locked => false).order("created_at DESC")

describe Job, ":recents_available" do

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

it "should include recents jobs that are available and unlocked" do
  @job = Factory(:job, :available => true, :locked => false  )      
  Job.recents_available.should include(@job)
end

结束

推荐答案

David Chelimsky(Rspec 的创建者)在 中提供了以下示例Rspec Google 群组:

David Chelimsky (Rspec's creator) offered up the following example in the Rspec Google Group:

describe User, ".admins" do 
  it "includes users with admin flag" do 
    admin = User.create! :admin => true 
    User.admin.should include(admin) 
  end

  it "excludes users without admin flag" do 
    non_admin = User.create! :admin => false 
    User.admin.should_not include(non_admin) 
  end 
end

class User < ActiveRecord::Base 
  named_scope :admins, :conditions => {:admin => true} 
end 

这显然与您的示例不同,但它应该让您知道如何去做.上下文的相关线程在这里:http://groups.google.com/group/rspec/browse_thread/线程/6706c3f2cceef97f

It's obviously not the same example as yours, but it should give you an idea of how to do it. The relevant thread for context is here: http://groups.google.com/group/rspec/browse_thread/thread/6706c3f2cceef97f

这篇关于如何测试范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆