2016-10-19 3 views
0

J'utilise le MailForm Gem pour créer un formulaire de contact pour mon application et tout semble fonctionner très bien, j'ai donc décidé d'écrire quelques tests pour s'assurer qu'il reste comme ça.Rails MailForm Gem captcha validation ne pas passer le test

class ContactsControllerTest < ActionDispatch::IntegrationTest 
    def setup 
    ActionMailer::Base.deliveries.clear 
    end 

    test "should send contact email" do 
    get contact_path 
    post contacts_path, params: { contact: { 
       name: "interested customer", 
       email: "[email protected]", 
       subject: "we are interested!", 
       message: "we are so interested!!!" 
    }} 
    assert_equal 1, ActionMailer::Base.deliveries.size 
    assert_redirected_to root_path 
    end 

    test "should not send invalid contact email" do 
    get contact_path 
    post contacts_path, params: { contact: { 
       name: "", 
       email: "", 
       subject: "", 
       message: "" 
    }} 
    assert_equal 0, ActionMailer::Base.deliveries.size 
    assert_template 'contacts/new' 

    end 

    test "should not send contact email with captcha filled" do 
    get contact_path 
    post contacts_path, params: { contact: { 
       name: "interested customer", 
       email: "[email protected]", 
       subject: "we are interested!", 
       message: "we are so interested!!!", 
       nickname: "not_blank" 
    }} 
    assert_equal 0, ActionMailer::Base.deliveries.size 
    assert_template 'contacts/new' 
    end 

les deux premiers tests passent tandis que le troisième échoue avec le message

FAIL["test_should_not_send_contact_email_with_captcha_filled", ContactsControllerTest, 5.2574800989968935] 
    test_should_not_send_contact_email_with_captcha_filled#ContactsControllerTest (5.26s) 
    Expected: 0 
     Actual: 1 
    test/controllers/contacts_controller_test.rb:35:in `block in <class:ContactsControllerTest>' 

Mon modèle ressemble à ceci. Ma première pensée était que la validation captcha n'empêche pas l'envoi du courrier. Si quelqu'un pouvait signaler ce qui me manquait, je l'apprécierais.

Répondre

0

Le modèle mailform renvoie valide, même si le surnom est donné. Mais vous pouvez vérifier s'il s'agit de spam, ce qui interdit l'envoi du courrier. Je l'utilise pour vérifier si la détection de spam fonctionne (avec rspec):

it 'should be marked spam if it contains :nickname' do 
expect(FactoryGirl.build(:contact_with_nickname)).to be_spam 
end