rspec

2013-11-14 7 views
25

के साथ एक्शनमेलर परीक्षण मैं रेल 4 एप्लिकेशन विकसित कर रहा हूं जिसमें ईमेल भेजने/प्राप्त करना शामिल है। उदाहरण के लिए, मैं उपयोगकर्ता पंजीकरण, उपयोगकर्ता टिप्पणी, और ऐप में अन्य घटनाओं के दौरान ईमेल भेजता हूं।rspec

मैं कार्रवाई mailer का उपयोग कर सभी ईमेल बनाया है, और मैं परीक्षण के लिए rspec और shoulda इस्तेमाल किया। मुझे यह जांचने की ज़रूरत है कि उचित उपयोगकर्ताओं को मेल सही तरीके से प्राप्त किए गए हैं या नहीं। मुझे नहीं पता कि व्यवहार का परीक्षण कैसे करें।

कृपया मुझे दिखाएं कि shoulda और rspec का उपयोग करके मुझे कैसे दिखाएं।

+14

यह सवाल ऑफ-विषय के रूप में क्यों बंद है? –

उत्तर

41

आरएसपीईसी का उपयोग करके एक्शनमेलर का परीक्षण करने के तरीके पर good tutorial है। यह वह अभ्यास है जिसका मैंने पालन किया है और यह अभी तक मुझे विफल नहीं हुआ है।

निम्नलिखित Notifier मेलर और User मॉडल मानते हुए:

ट्यूटोरियल दोनों रेल 3 और 4

मामले में टूट जाता है ऊपर के लिंक में ट्यूटोरियल के लिए काम करेंगे, यहाँ प्रासंगिक हिस्से हैं

class Notifier < ActionMailer::Base 
    default from: '[email protected]' 

    def instructions(user) 
    @name = user.name 
    @confirmation_url = confirmation_url(user) 
    mail to: user.email, subject: 'Instructions' 
    end 
end 

class User 
    def send_instructions 
    Notifier.instructions(self).deliver 
    end 
end 

और निम्न परीक्षण विन्यास:

# config/environments/test.rb 
AppName::Application.configure do 
    config.action_mailer.delivery_method = :test 
end 
इस विषय पर मूल ब्लॉग पोस्ट के लिए लुकास Caton को

# spec/models/user_spec.rb 
require 'spec_helper' 

describe User do 
    let(:user) { User.make } 

    it "sends an email" do 
    expect { user.send_instructions }.to change { ActionMailer::Base.deliveries.count }.by(1) 
    end 
end 

# spec/mailers/notifier_spec.rb 
require 'spec_helper' 

describe Notifier do 
    describe 'instructions' do 
    let(:user) { mock_model User, name: 'Lucas', email: '[email protected]' } 
    let(:mail) { Notifier.instructions(user) } 

    it 'renders the subject' do 
     expect(mail.subject).to eql('Instructions') 
    end 

    it 'renders the receiver email' do 
     expect(mail.to).to eql([user.email]) 
    end 

    it 'renders the sender email' do 
     expect(mail.from).to eql(['[email protected]']) 
    end 

    it 'assigns @name' do 
     expect(mail.body.encoded).to match(user.name) 
    end 

    it 'assigns @confirmation_url' do 
     expect(mail.body.encoded).to match("http://aplication_url/#{user.id}/confirmation") 
    end 
    end 
end 

प्रॉप्स: 0

ये चश्मा आप क्या चाहते हैं आप मिलना चाहिए।

+3

लेकिन यदि आप उपयोगकर्ता.send_instructions से अपवाद पकड़ते हैं और अपवाद के साथ स्वयं को एक ईमेल भेजते हैं, तो इससे कोई समस्या नहीं आती है। आप बस परीक्षण करें कि * कोई * ई-मेल भेजा गया है, न कि आपके विशिष्ट। – Phillipp

+2

@ फिलिप ने एक अच्छा बिंदु बनाया, और यदि आप विशिष्ट मेल का परीक्षण करना चाहते हैं, तो 'ActionMailer :: Base.deliveries' 'Mail :: Message' ऑब्जेक्ट्स की एक सरणी है। [मेल :: संदेश API] का संदर्भ लें (http://www.rubydoc.info/github/mikel/mail/Mail/Message)। – janechii

+0

उन लोगों के लिए जो आश्चर्य करते हैं कि 'mock_model' क्यों काम नहीं करता है: http://stackoverflow.com/a/24060582/2899410 – 707