mail_form gem rspec“未定义方法`电子邮件'for”

时间:2017-12-29 02:46:08

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

如何编写测试mail_form gem? Вut测试给出错误。 如何关联OrderForm和MailForm? 我有 : 模型:

class OrderForm < MailForm::Base
  attributes :name
  attributes :email
  attributes :phone_number
  attributes :order_name
  attributes :address
  attributes :file_1, attachment: true
  def mail_attachments
    [:file_1]
  end

控制器,其中:

class OrdersController < ApplicationController

  def create
    @order_form = OrderForm.new(params[:order_form])
    @order_form.deliver
    redirect_to root_path, notice: 'Заявка отправлена'
  end
end

结束视图(发送信件的形式): 订单/ new.html.haml_spec.rb

   =form_for @order_form, url: orders_path, method: :post do |f|
         =f.text_field :name, placeholder: 'ФИО', class: 'gui-input'
         =f.text_field :email, placeholder: 'Email', class: 'gui-input'
         =f.text_field :phone_number, placeholder: 'Номер телефона', 

结束我写测试:

require 'rails_helper'
RSpec.describe "orders/new", type: :view do
    it do
        assign(:order, build(:order))
        render
      expect(rendered).to have_field :name
    end
end

1 个答案:

答案 0 :(得分:1)

检查rake routes的输出,网址orders/new将执行orders#new操作并呈现视图orders/new.html.erb

GET /orders/new orders#new  return an HTML form for creating a new order

new.html.erb将使用orders#new操作中的变量。你缺少那些变量

class OrdersController < ApplicationController

  def new 
    @order_form = OrderForm.new
  end

  def create
    @order_form = OrderForm.new(params[:order_form])
    @order_form.deliver
    redirect_to root_path, notice: 'Заявка отправлена'
  end
end