मेरा एप्लिकेशन नियंत्रक को ActiveRecord :: RecordNotFound का प्रचार कर रहा है जब कोई मॉडल रिकॉर्ड पुनर्प्राप्त नहीं कर सकता है।ActiveRecord के लिए टेस्ट :: RecordNotFound
यहाँ डीबी क्वेरी है:
def self.select_intro_verse
offset = rand(IntroVerse.count)
IntroVerse.select('line_one', 'line_two', 'line_three', 'line_four', 'line_five').offset(offset).first!.as_json
end
first!
एक ActiveRecord::RecordNotFound
अगर कोई रिकॉर्ड पाया जाता है को जन्म देती है, और मैं इसे बचाने और मेरे नियंत्रक में उचित खाका प्रस्तुत करना करने में सक्षम हूँ।
यह अपेक्षित व्यवहार है इसलिए मैं अपने चश्मे में इसके लिए एक परीक्षण करना चाहता हूं। मैं ने लिखा है:
context "verses missing in database" do
it "raises an exception" do
expect(VerseSelector.select_intro_verse).to raise_exception
end
end
जब मैं rspec चलाएँ:
1) VerseSelector verses missing in database raises an exception Failure/Error: expect(VerseSelector.select_intro_verse).to raise_exception ActiveRecord::RecordNotFound: ActiveRecord::RecordNotFound
मेरे लिए परीक्षण भले ही अपवाद उठाया है विफल रहता है! मैं अपना टेस्ट पास कैसे कर सकता हूं? rspec-expectations#expecting-errors
इस के लिए दस्तावेज में
+1 ईमानदार होना। :-) –
_must lambda या proc_ होना चाहिए - बोल्ड होना चाहिए :) – Fabio