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

終わり

4

2 に答える 2

24

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

于 2011-03-18T12:30:26.793 に答える
4

もっと洗練された解決策があると確信していますが、私は常に、自分のスコープに含める必要があるオブジェクトと含めないオブジェクトをいくつか設定してきました。スコープを呼び出した後、返されたオブジェクトに本来あるべきオブジェクトがあり、そうでないオブジェクトがないことを確認します。

どちらかといえば、他の回答で啓発されることを願っています。

于 2011-01-30T01:28:26.280 に答える