2012-12-07 12 views
20

में रिकॉर्ड के अस्तित्व की सुंदरता की जांच कैसे करें आरएसपीईसी में रिकॉर्ड के अस्तित्व की जांच करने का कोई बेहतर तरीका है?आरएसपीसी

Foo.where(bar: 1, baz:2).count.should == 1 
+1

तुम कोशिश कर सकते हैं ' Foo.where (बार: 1, बाज़: 2)। Should_not be_empty'? – Dogbert

उत्तर

2
Foo.where(bar: 1, baz: 2).exists?.should be_true 
4

उपयोग Foo.exists?(bar: 1, baz: 2).should be_true

28

Rspec 2.13.0 के साथ, मैं ऐसा करने में सक्षम था

Foo.where(bar: 1, baz: 2).should exist 

संपादित करें:

Rspec अब an expect syntax:

expect(Foo.where(bar: 1, bax: 2)).to exist 
+6

'उम्मीदवार (Foo.find_by_bar (1))। Be_present' जब आप खोजकर्ताओं का उपयोग कर रहे हों – Andrei

7

उम्मीद वाक्य रचना का उपयोग करना:

expect(Foo.where(bar: 1, baz: 2)).not_to be_empty 
expect(Foo.where(bar: 1, baz: 2)).to exist 
7

rspec-रेल> 3.0

एक ब्लॉग मॉडल होने के लिए,

describe 'POST #create' do 

    it "creates a post" do 

     post :create, :blog => { title: "blog title"} 

     #returns true if the post was successfully added 
     expect(Blog.where(title: "blog title")).to be_present 
    end 
    end