我有一个按钮可以打开一个模态窗口,其中包含一个发送电子邮件的简单表单。唯一的字段是消息,我的控制器将隐藏的电子邮件填充到字段并通过电子邮件发送。表单只发送一条消息,因此没有模型。
如何编写测试以验证是否已发送电子邮件?
我的路线
match '/contact_guide', to: 'contact#contact_guide', via: 'post'
控制器
def contact_guide
# Send mail to a guide.
email_from = params[:email_from]
email_to = params[:email_to]
body = params[:message]
name = params[:name]
ContactMailer.contact_guide(name, email_to, email_from, body).deliver_now
flash[:success] = 'Message sent'
# Go back to the guide page.
u = User.find_by(email: email_to)
g = u.guide
redirect_to guide_url(g)
end
表单HTML
<%= form_tag(contact_guide_path) do %>
<div class="form-group">
<%= label_tag 'message', 'Contact Guide' %>
<%= text_area_tag 'message', nil, class: 'form-control',
rows: 4, placeholder: 'Message...', :required => true %>
<!-- Hidden to email field -->
<%= label_tag 'email_to', 'Email', style: 'visibility:hidden;display:none' %>
<%= email_field_tag 'email_to', nil, class: 'form-control',
style: 'visibility:hidden;display:none', value: @guide.user.email %>
<!-- Hidden from email field -->
<%= label_tag 'email_from', 'Email', style: 'visibility:hidden;display:none' %>
<%= email_field_tag 'email_from', nil, class: 'form-control',
style: 'visibility:hidden;display:none', value: @email_from %>
<!-- Hidden guide name field -->
<%= label_tag 'name', 'Name', style: 'visibility:hidden;display:none' %>
<%= text_field_tag 'name', nil, class: 'form-control',
style: 'visibility:hidden;display:none', value: @guide.user.name %>
<%= submit_tag "Send Message", class: 'btn btn-default btn-about contact-btn' %>
</div>
<% end %>
在我的测试中,我试图这样做
post contact_guide_path, {message: "This is a message."}
但是我收到了错误
ArgumentError Exception: An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.
答案 0 :(得分:1)
在post语句中添加一些缺少的参数。
post contact_guide_path, {message: "This is a message."}
转到:
post contact_guide_path, {message: "This is a message.",email_to: "[testing email]", email_from: "[Testint email]" , name: 'ABC'}