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

Rails MailForm Gem captcha validation not passing test

我正在使用 邮件表格 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

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

邮件表单模型returns有效,即使给出了昵称。但是可以查看是否是垃圾邮件,禁止发送邮件。 我用它来检查垃圾邮件检测是否有效(rspec):

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