Rails MailForm Gem验证码验证未通过测试

时间:2016-10-19 18:19:41

标签: ruby-on-rails-5 mail-form

我正在使用 MailForm Gem 为我的应用程序创建一个联系表单,一切似乎工作正常,所以我决定写一些测试,以确保它保持这种方式。

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: "interested@customer.com",
                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: "interested@customer.com",
                subject: "we are interested!",
                message: "we are so interested!!!",
                nickname: "not_blank" 
    }}  
    assert_equal 0, ActionMailer::Base.deliveries.size
    assert_template 'contacts/new'
  end

前两个测试通过,而第三个测试失败并显示消息

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>'

我的模特看起来像这样。

class Contact < MailForm::Base
   attribute :name,      :validate => true
   attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+  ([\w]{2,})\z/i
   attribute :subject
   attribute :message
   attribute :nickname, :captcha => true

   def headers
    {
      :subject => "Contact: #{subject}" ,
      :to => "myemail@mail.com",
      :from => %("#{name}" <#{email}>)
     }
    end
end

我的第一个想法是验证码验证并未阻止邮件被发送。如果有人能够指出我所缺少的东西,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

即使给出了昵称,mailform模型也会返回有效。但是你可以检查它是否是垃圾邮件,禁止发送邮件。 我用它来检查垃圾邮件检测是否有效(使用rspec):

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