2011-01-30 70 views
17

試圖找到但沒有成功。只是想知道我怎麼能在Rails 3中測試示波器。如何測試示波器?

可能使用rspec,shoulda或者只是一個測試單元。

謝謝。

其實,我試過這種方式,但它不是完整的測試,因爲它仍然需要放置order()方法。

範圍:

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 

回答

25

大衛赫利姆斯基(Rspec的創建者)在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/thread/6706c3f2cceef97f

4

我敢肯定有一個更好的解決方案,但我一直只是設置一些對象應該和不應該在我的範圍。在調用範圍之後,我檢查返回的對象是否應該,並且沒有它不應該的對象。

如果有任何問題,我希望能夠通過其他答案獲得啓發。

+0

設置兩個邊界情況並測試你的方法(這是什麼範圍真的是一個優雅的測試解決方案,所以不要擔心使用上述答案 – Houen 2011-08-27 12:17:42