我使用mail form gem来处理我的网站的联系表单。我在我的测试环境中使用了rspec和capybara。我在尝试测试联系表单页面时遇到错误。联系人控制器的新操作确实创建了联系模型的新实例,但是我不知道如何在rspec中执行此操作来修复失败的原因。
测试输出失败:
No route matches {:action=>"new", :controller=>"contacts", :format=>nil} missing required keys: []
contact_form_spec.rb
require 'spec_helper'
require 'rails_helper'
RSpec.feature "Comtact Form Email", :type => :feature do
scenario "User sends email via contact form" do
visit contacts_path
fill_in 'Name', :with => 'Jony Ive'
fill_in 'Email', :with => 'jony@apple.com'
fill_in 'Subject', :with => 'Subject Here'
fill_in 'Message', :with => 'Message'
click_button 'Send Message'
expect(page).to have_text("Thank you for your message.")
last_email.to.should include('help@everydayrails.com')
last_email.from.should include('aaron@everydayrails.com')
end
end
contacts_controller.rb
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
@contact.request = request
if @contact.deliver
flash[:notice] = 'Thank you for your message. We will contact you soon!'
redirect_to contacts_path
else
flash[:error] = 'Your message could not be sent.'
render :new
end
end
end
的routes.rb
match '/contacts', to: 'contacts#new', via: 'get'
resources :contacts, only: [:create]
路线输出
contacts GET /contacts(.:format) contacts#new
POST /contacts(.:format) contacts#create