How to test scopes?

后端 未结 2 1684
心在旅途
心在旅途 2021-02-04 04:41

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

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

Thanks.

Actually

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-04 05:13

    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 
    

    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

提交回复
热议问题